Decompress/unzip java objects
TheĀ java.util.zip.ZipEntry extends the Zip file entry. java.util.zip.ZipEntry implementsĀ java.util.zip.ZipConstants interface.
In this sample code we pass the zip file name as an argument/parameter in the command line argument .
To unzip, create object byte stream and give the size of an array perceiving the length of the text in the file
import java.io.*;import java.util.zip.*;public class UnZipObjectSample {public static void main(String arguments[]) {try {BufferedOutputStream bufferedOutputStream = null;ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(arguments[0])));ZipEntry zipEntry;while ((zipEntry = zipInputStream.getNextEntry()) != null) {int counter;int initial = 0;byte text[] = new byte[900];bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(”sampleOUT.txt”),900);while ((counter = zipInputStream.read(text, initial, 900)) != -1) {bufferedOutputStream.write(text, initial, counter);}//flushing and closing bufferedOutputStreambufferedOutputStream.flush();bufferedOutputStream.close();}} catch (Exception exception) {//handle generic exceptionsexception.printStackTrace();}}}import java.io.*;
import java.util.zip.*;
public class UnZipObjectSample {
public static void main(String arguments[]) {
try {
BufferedOutputStream bufferedOutputStream = null;
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(
new FileInputStream(arguments[0])));
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
int counter;
int initial = 0;
byte text[] = new byte[900];
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(”sampleOUT.txt”),
900);
while ((counter = zipInputStream.read(text, initial, 900)) != -1) {
bufferedOutputStream.write(text, initial, counter);
}
//flushing and closing bufferedOutputStream
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
} catch (Exception exception) {
//handle generic exceptions
exception.printStackTrace();
}
}
}