|
The following gives an example which unzips a ZIP file:
//
Written by W.Buchanan, 2003
import java.io.*;
import java.util.zip.*;
public class
unzip {
public static void main (String argv[]) {
try {
BufferedOutputStream destination = null;
FileInputStream finps = new FileInputStream(argv[0]);
ZipInputStream zipinp = new ZipInputStream(new BufferedInputStream(finps));
ZipEntry entry;
while((entry = zipinp.getNextEntry()) != null) {
System.out.println("UnZipping: " + entry);
int count;
byte databuff[] = new byte[2048];
// write the files
FileOutputStream fos = new FileOutputStream(entry.getName());
destination = new BufferedOutputStream(fos, 2048);
while ((count = zipinp.read(databuff, 0, 2048)) !=
-1) {
destination.write(databuff, 0, count);
}
destination.flush();
destination.close();
}
zipinp.close();
} catch(Exception e) {
}
}
}
|
In Java the java.util.zip package contains the ZipInputStream
stream for sequentially read-ing ZIP files. The constructor
for this class accepts a FileInputStream object, such as:
FileInputStream
finps = new FileInputStream(argv[0]);
ZipInputStream zipinp = new ZipInputStream(new BufferedInputStream(finps));
On opening the ZIP input stream, each entry is read, one
at a time, for this the getNextEntry() method returns a
ZipEntry() object. Once the end-of-the-file is ready, the
getNextEntry() method returns a null.
while((entry =
zipinp.getNextEntry()) != null) {
// main code
}
The uncompressed output stream can then be setup inside
the while loop, such as:
FileOutputStream
fos = new FileOutputStream(entry.getName());
destination = new BufferedOutputStream(fos, 2048);
where entry.getName() returns the name of the compressed
file. Next the zipped data can be read and written to the
uncompressed stream, with:
while ((count
= zipinp.read(databuff, 0, 2048)) != -1) {
destination.write(databuff, 0, count);
}
where the read() method reads from the ZIP’ed data
stream, and the write() method writes to the uncompressed
data stream. The count variable is used to define the number
of bytes that have been read from the ZIP stream, else it
will return a –1 when the end-of-file is reached.
The uncompressed stream is then closed with:
destination.flush();
destination.close();
and the input stream with:
zipinp.close();
An example run is shown next:
C:\java>javac
unzip.java
C:\java>java
unzip 1.zip
UnZipping: billnotebook_test.txt
ZIP'ing a file
To zip a file:
import java.io.*;
import java.util.zip.*;
public class
zip {
public static void main (String argv[]) {
try {
FileOutputStream
fouts = new FileOutputStream("out.zip");
ZipOutputStream targetStream = new ZipOutputStream(fouts);
targetStream.setMethod(ZipOutputStream.DEFLATED);
String inputFileName
= argv[0];
System.out.println("ZIP'ing: " + inputFileName);
FileInputStream
fis = new FileInputStream(inputFileName);
BufferedInputStream
sourceStream = new BufferedInputStream(fis);
int count;
byte data[] = new byte[2048];
ZipEntry
Entry = new ZipEntry(inputFileName);
targetStream.putNextEntry(Entry);
while((count
= sourceStream.read(data, 0, 2048)) != -1) {
System.out.println(".");
targetStream.write(data, 0, count);
}
sourceStream.close();
targetStream.close();
} catch(Exception e) {
}
}
}
|
The ZipOutputStream class is used to create a stream for
the output ZIP file, along with the java.util.zip package.
The constructor for the ZipOutputStream is from the OutputStream
object. For example to create an output file named out.zip:
FileOutputStream
fouts = new FileOutputStream("out.zip");
ZipOutputStream targetStream = new ZipOutputStream(fouts);
targetStream.setMethod(ZipOutputStream.DEFLATED);
The setMethod() method with a value of DEFLATED defines
that the file will be stored in a compressed format (which
is the default, anyway). It is also possible to define the
level of the compression with the setLevel() method, where:
obj.setLevel(int
Lev);
is used to define the level of the compression, where Lev
ranges from 1 to 9, where 1 is the weakest compression level,
and 9 is the strongest.
Initially the target ZIP output stream is created, with:
String inputFileName
= argv[0];
System.out.println("ZIP'ing: " + inputFileName);
FileInputStream fis = new FileInputStream(inputFileName);
BufferedInputStream
sourceStream = new BufferedInputStream(fis);
The input argument will be passed into argv[0]. Next the
original file details, such as the file-name, its original
size, compressed size, and so on, must be stored. This is
achieved with the ZipEntry() method, such as:
ZipEntry Entry
= new ZipEntry(inputFileName);
This is then put into the file with putNextEntry() method:
targetStream.putNextEntry(Entry);
After this the input data stream can be read, and written
to the output ZIP stream with:
while((count =
sourceStream.read(data, 0, 2048)) != -1) {
System.out.print(".");
targetStream.write(data, 0, count);
}
after which the data streams can be closed with:
sourceStream.close();
targetStream.close();
A sample run gives:
C:\java>javac
zip.java
C:\java>java
zip 1.txt
ZIP'ing: 1.txt
..
|