Introduction: The Java platform provides
powerful API to work with the most popular compressed file format – zip format.
The ZIP API:
The java.util.zip package provides classes for working with zip file format.
The key classes
in java.util.zip package are listed as following:
ZipInputStream: this is the main class which can be
used for reading zip file and extracting files and directories (entries) within
the archive. Here are some important usages of this class:
§ read a zip via its constructor ZipInputStream(FileInputStream)
§ read entries of files and directories
via method getNextEntry()
§ read binary data of current entry via
method read(byte)
§ close current entry via method closeEntry()
§ close the zip file via method close()
ZipEntry: this class represents an entry in the
zip file. Each file or directory is represented as a ZipEntry object. Its method getName() returns a String which represents path
of the file/directory.
FileOutputStream class is used to write
content of the current ZipEntry to a file on disk, via its
method write(byte[] bytes,
int offset, int length).
ZipOutputStream: this class allows writing files
into a zip file. It accepts an OutputStream object in the constructor ZipOutputStream(OutputStream
out); The putNextEntry() method allows to append a file at the end of the zip
file.
JavaCode to create Zip File:
For this example I created “test” and
“zip” folders under c:\drive. I placed three .txt files under “test”
folder. The following code creates a Zip file using these three .txt files and place
it at c:\test\zip\test.zip.
import
java.io.File;
import
java.io.FileInputStream;
import java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.util.zip.ZipEntry;
import
java.util.zip.ZipOutputStream;
public class
zipFile {
public static void
main(String[] args) {
//source of files that need to be zipped
String[] textfiles = { "C:/test/test1.txt", "C:/test/test2.txt", "C:/test/test3.txt"};
//place to create a zip file
String zipFile = "C:/test/zip/test.zip";
try {
// create byte buffer
byte[]
buffer = new byte[1024];
//create FileOutputStream to write a file with
specified file path and name
FileOutputStream out =
new FileOutputStream(zipFile);
//create ZipOutputStream from FileOutputStream to
filter output stream in zip format
ZipOutputStream
zipout = new ZipOutputStream(out);
//loop through each file
for (int i=0; i <
textfiles.length; i++) {
//create a new
file instance for specified file
File srcFile
= new File(textfiles[i]);
System.out.println("Adding: "+srcFile.getName());
// create a FileInputStream to open a file
FileInputStream in = new FileInputStream(srcFile);
// create a ZipEntry with file name and start writing
to ZipOutputStream.
zipout.putNextEntry(new ZipEntry(srcFile.getName()));
int length;
//read up to 1024 bytes from the file into an array of
byte and write the data in current ZipEntry
while
((length = in.read(buffer)) > 0) {
zipout.write(buffer, 0, length);
}
//close zipEntry
zipout.closeEntry();
// close the InputStream
in.close();
}
// close the ZipOutputStream
zipout.close();
System.out.println("All files are zipeed to " + zipFile);
}
catch
(IOException ioe) {
System.out.println("Error creating zip file: " + ioe);
}
}
}
Output:
Adding: test1.txt
Adding: test2.txt
Adding: test3.txt
All files are zipeed to C:/test/zip/test.zip
Java Code to UnZip File:
I created a folder called “unzip” under
c:\test. I used the zip file I created in previous
example(c:\test\zip\test.zip) and unzipped the files to c:\test\unzip folder.
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import java.sql.Date;
import
java.util.zip.ZipEntry;
import
java.util.zip.ZipInputStream;
public class
unzipFile {
public static void
main(String[] args)
{
try {
//zip file needs to be extracted
String zipFile="c:/test/zip/test.zip";
//destination folder where you need to unzip
String dest="c:/test/unzip";
//create InputStream object to open a file
FileInputStream
in=new FileInputStream(zipFile);
//Create ZipInputStream object to read Zip file
ZipInputStream
zipin=new ZipInputStream(in);
//each file/directory is represented as entry. Read
entries of files/directories
ZipEntry
entry=zipin.getNextEntry();
while(entry!=null)
{
//file name
String
entryName=entry.getName();
System.out.println("Extracting: " + entryName + " To
" + dest);
//extracted file location & name
File
extractedFile=new File(dest+ File.separator + entryName);
//extractedFile.getParentFile().mkdirs();
//create FileOutputStream Object
FileOutputStream
out=new FileOutputStream(extractedFile);
int len;
byte []
buffer=new byte[1024];
while((len=zipin.read(buffer))
>0)
{
//write content of current entry to a file
out.write(buffer,0,len);
}
out.close();
System.out.println("extracted:" + extractedFile);
entry=zipin.getNextEntry();
}
zipin.closeEntry();
zipin.close();
System.out.println("Unzipping is complete");
}
catch(IOException
io)
{
io.printStackTrace();
}
}
}
Output:
Extracting: test1.txt To c:/test/unzip
extracted:c:\test\unzip\test1.txt
Extracting: test2.txt To c:/test/unzip
extracted:c:\test\unzip\test2.txt
Extracting: test3.txt To c:/test/unzip
extracted:c:\test\unzip\test3.txt
Unzipping is complete
No comments:
Post a Comment