Java program for zipping.Download the file given as attachment.
unzip the file and store it in any folder in ur hard-drive....
go to command prompt....
set the classpath properly.
C:\Zipper\[if the files are stored in this folder, then set the classpath in the following manner]
C:\Zipper>set classpath=.
then type
C:\Zipper>java Zipper
Do u want to make zip file(Y/N)?
y
Do u want to add to archive(Y/N)?
y
Give the file name with path:E:\a
Do u want to add to archive(Y/N)?
y
Give the file name with path:E:\b.txt
Do u want to add to archive(Y/N)?
n
Enter the location where the zip file will be located:
E:\
Enter the zip file name:///Don't give any extension
testZip
Creating Zip file....Wait..........
Zip file created....
Also read the readme file carefully.
Please Rate..........................
Code: JAVA
import java.io.*;
import java.util.zip.*;
import java.util.*;
class Zipper
{
public static void main(String[] args)
{
Vector v=new Vector(1,1);
String str;
String zipLocation="";
String zipName="";
int i=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Do u want to make zip file(Y/N)?");
str=br.readLine();
if(str.equalsIgnoreCase("Y"))
while(true)
{
System.out.println("Do u want to add to archive(Y/N)?");
str=br.readLine();
if(str.equalsIgnoreCase("Y"))
{
System.out.print("Give the file name with path:");
v.add(br.readLine());
System.out.println();
}
else if(str.equalsIgnoreCase("N"))
{
System.out.println("Enter the location where the zip file will be located:");
zipLocation=br.readLine();
System.out.println("Enter the zip file name:");
zipName=br.readLine();
if(zipLocation.length()==0)
{
System.out.println("The zip file location has not been enterred...");
throw new Exception();
}
else if(zipName.length()==0)
{
System.out.println("The zip file location has not been enterred...");
throw new Exception();
}
break;
}
else
{
System.out.println("The input parameter is not recognized.");
continue;
}
}
else if(str.equalsIgnoreCase("N"))
System.exit(0);
else
{
System.out.println("Invalid input parameter.Exiting from program.......");
System.exit(0);
}
File[] listToBeZipped=new File[v.size()];
for(i=0;i<v.size();i++)
listToBeZipped[i]=new File((v.get(i)).toString());
FileOutputStream fout=new FileOutputStream(zipLocation+"\\"+zipName+".zip");
CheckedOutputStream csum=new CheckedOutputStream(fout,new Adler32());
ZipOutputStream zos=new ZipOutputStream(csum);
//BufferedOutputStream bwr=new BufferedOutputStream(zos);
System.out.println("Creating Zip file....Wait..........");
ZippingUtility obZipUtil=new ZippingUtility(listToBeZipped,zipLocation,zipName,zos);
zos.close();
System.out.println("Zip file created....");
}
catch(ZipException e)
{
System.out.println("No file selected for zipping");
}
catch(Exception e)
{
System.out.println("Exception occurred in main()."+e);
e.printStackTrace();
}
}
}
class ZippingUtility
{
private File[] listToBeZipped;
private String zipLocation;
private String zipName;
//private BufferedOutputStream bwr;
private ZipOutputStream zos;
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
ZippingUtility(File[] listToBeZipped,String zipLocation,String zipName,ZipOutputStream zos)
{
this.listToBeZipped=new File[listToBeZipped.length];
for(int i=0;i<listToBeZipped.length;i++)
this.listToBeZipped[i]=listToBeZipped[i];
this.zipLocation=zipLocation;
this.zipName=zipName;
this.zos=zos;
//this.bwr=new BufferedOutputStream(zos);
//this.bwr=bwr;
ZipBegin();
}
void ZipBegin()
{
try
{
for(int i=0;i<listToBeZipped.length;i++)
{
if(!listToBeZipped[i].exists())
{
System.out.println("The file "+ listToBeZipped[i]+" does not exist.");
continue;
}
if(listToBeZipped[i].isDirectory())
{
File[] list=listToBeZipped[i].listFiles();
new ZippingUtility(list,zipLocation,zipName,zos);
}
else
{
FileInputStream fis = new FileInputStream(listToBeZipped[i]);
//create a new zip entry
ZipEntry anEntry = new ZipEntry(listToBeZipped[i].getPath());
//place the zip entry in the ZipOutputStream object
zos.putNextEntry(anEntry);
//now write the content of the file to the ZipOutputStream
while((bytesIn = fis.read(readBuffer)) != -1)
{
zos.write(readBuffer, 0, bytesIn);
}
//close the Stream
fis.close();
/*FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
int c;
ZipEntry zen=new ZipEntry(f.getPath());
zos.putNextEntry(zen);
while((c=br.read())!=-1)
bwr.write(c);
br.close();*/
}
}
}
catch(FileNotFoundException e)
{
System.out.println("The files you entered does not exist.");
System.exit(1);
}
catch(Exception e)
{
System.out.println("Exception occurred..");
e.printStackTrace();
}
}
}