In this example we are going to see Extract Contents Of Zip File In Java.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class JavaZipExtractContent { public static void main(String[] args) throws Exception { ZipInputStream inStream = new ZipInputStream(new FileInputStream("compressed.zip")); OutputStream outStream = new FileOutputStream("extracted.txt"); byte[] buffer = new byte[1024]; int read; ZipEntry entry; if ((entry = inStream.getNextEntry()) != null) { while ((read = inStream.read(buffer)) > 0) { outStream.write(buffer, 0, read); } } outStream.close(); inStream.close(); } } |
ZipInputStream is the main class used for reading zip file and extracting files and directories.