Wednesday, August 22, 2007

ZIP Files in Java

The ZIP file format has become extremely popular for the distribution and storage of files. ZIP files allow for compression, making transporting a set of files from one location to another faster.Depending on the source file types being compressed, ZIP files can provide significant space savings. ZIP archives can also maintain the directory structure of files that are compressed, making the ZIP format a formidable file transport mechanism.

The java.util.zip package allows for the programmatic reading and writing of the ZIP and GZIP formats.Learning how to use the offerings of the java.util.zip package might best be facilitated via example.

The below example FileZip class has only one method makeZip(), takes two parameters,
String Array- Pass the files paths as strings which you want to Zip.
String - The target zip file name to save as.

Code:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipFiles {
public String makeZip(String[] filesToZip,String zipFileName){
byte[] buffer = new byte[18024];
if(!zipFileName.endsWith(".zip")){
zipFileName+=".zip";
}
try {
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
// Set the compression ratio
out.setLevel(Deflater.DEFAULT_COMPRESSION);
// iterate through the array of files, adding each to the zip file
int filesSiz=filesToZip.length;
for (int i = 0; i < style="color: rgb(0, 153, 0);">// Associate a file input stream for the current file
File file2Zip=new File(filesToZip[i].toString());
InputStream in = new BufferedInputStream(new FileInputStream(file2Zip));
// Add ZIP entry to output stream.
String fileExctNm=filesToZip[i].substring(filesToZip[i].lastIndexOf("/")+1);
out.putNextEntry(new ZipEntry(fileExctNm));
// Transfer bytes from the current file to the ZIP file
int len;
while ((len = in.read(buffer)) > 0)
{
out.write(buffer, 0, len);
}
// Close the current entry
out.closeEntry();
// Close the current file input stream
in.close();
}
// Close the ZipOutPutStream
out.close();
}
catch (IllegalArgumentException iae) {
iae.printStackTrace();
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
return new File(zipFileName).toString();
}
}

3 comments:

Alex said...

Zip files have many singularities and one of them they are often damaged and consequently to fight with problem there is-zip archive crc repair,it is free to the best of my memory,it can also extract needed document from corrupted archive and repair it,will successfully recover all documents,works faster and recovers more documents, than other services,program works under all supported versions of Microsoft Windows operating system,will mark all files for zip crc failed repair and zip archive crc repair,repair zip files crc error and you'd like to save recovered data,also possible to select your Zip file from the list of previously recovered archives.

Alex said...

Zip files to my mind one of the best important files,because I have on my computer near 2 000 zip files,but not so long approximately 100 zip files was corrupted and in inet I found this tool-crc zip files repair.It helped me surprisingly.Tool is free as far as I know and it can extract needed document from corrupted archive and repair it,moreover utility repair zip files crc error and you'd like to save recovered data.

Alexis said...

On my computer there are a lot of zip files.And yesterday all files were deleted no one know how.But I asked to friend about this situation.And he advised-repair zip file cyclic redundancy,this advice was successeful and tool solved my problems in a minute for free!!