2 Examples To Read Cypher Files Inwards Java, Zipfile Vs Zipinputstream

ZIP format is 1 of the most pop compression machinery inwards estimator world. Influenza A virus subtype H5N1 Zip file may contains multiples files or folder inwards compressed format.  Java API provides extensive back upwards to read Zip files, all classes related to zip file processing are located inwards java.util.zip package. One of the  most mutual chore related to zip archive is to read a Zip file as well as display what entries it contains, as well as therefore extract them inwards a folder. In this tutorial nosotros volition larn how to do this chore inwards Java. There are 2 ways yous tin iterate over all items inwards a given zip archive, yous tin role either java.util.zip.ZipFile or java.util.zip.ZipInputStream. Since a Zip file contains several items, each of them has header plain containing size of items inwards set out of bytes. Which agency yous tin iterate all entries without truly decompressing the zip file.

The ZipFile class accepts a java.io.File or String file name, it opens a ZIP file for reading as well as UTF-8 charset is used to decode the entry names as well as comments.

Main do goodness of using ZipFile over ZipInputStream is that it uses random access to iterate over dissimilar entries, land ZipInputStream is sequential, because it plant  with stream, due to which it's non able to motion positions freely.

It has to read as well as decompress all zip information inwards club to make EOF for each entry as well as read header of side past times side entry. That's why its ameliorate to role ZipFile class over ZipInputStream for iterating over all entries from archive.

We volition larn to a greater extent than nearly how to role read Zip file inwards Java, past times next an example. By the way, code should run with zip file created past times whatever zip utility e.g. WinZip, WinRAR or whatever other tool, .ZIP format permits multiple compression algorithms.. I receive got tested amongst Winzip inwards Windows 8, only it should run amongst zip file created past times whatever tool.



Reading Zip archive inwards Java

In this example, I receive got used ZipFile class to iterate over each file from Zip archive. getEntry() method of ZipFile returns an entry, which has all meta information including name, size as well as modified appointment as well as time.


You tin inquire ZipFile for InputStream corresponding to this file entry for extracting existent data. Which means, yous solely incur terms of decompression, when yous truly quest to. By using java.util.zip.ZipFile, yous tin cheque each of entry as well as solely extract sure enough entries, depending upon your logic.

ZipFile is expert for both sequential as well as random access of private file entries. On the other hand, if yous are using ZipInptStream therefore similar any other InputStream, yous volition quest to procedure all entries sequentially, equally shown inwards instant example.

Key indicate to remember, particularly if yous are processing large zip archives is that, Java half dozen solely back upwards zip file upwards to 2GB. Thankfully Java vii supports zip64 mode, which tin endure used to procedure large zip file amongst size to a greater extent than than 2GB.

 ZIP format is 1 of the most pop compression machinery inwards estimator globe 2 Examples to read Zip Files inwards Java, ZipFile vs ZipInputStream

import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream;  /**  * Java plan to iterate as well as read file entries from Zip archive.  * This plan demonstrate 2 ways to retrieve files from Zip using ZipFile as well as past times using ZipInputStream class.  * @author Javin  */  public class ZipFileReader {      // This Zip file contains eleven PNG images     private static finally String FILE_NAME = "C:\\temp\\pics.zip";     private static finally String OUTPUT_DIR = "C:\\temp\\Images\\";     private static finally int BUFFER_SIZE = 8192;      public static void main(String args[]) throws IOException {          // Prefer ZipFile over ZipInputStream         readUsingZipFile();     //  readUsingZipInputStream();      }      /*      * Example of reading Zip archive using ZipFile course of written report      */      private static void readUsingZipFile() throws IOException {         finally ZipFile file = new ZipFile(FILE_NAME);         System.out.println("Iterating over zip file : " + FILE_NAME);          try {             finally Enumeration<? extends ZipEntry> entries = file.entries();             while (entries.hasMoreElements()) {                 finally ZipEntry entry = entries.nextElement();                 System.out.printf("File: %s Size %d  Modified on %TD %n", entry.getName(), entry.getSize(), new Date(entry.getTime()));                 extractEntry(entry, file.getInputStream(entry));             }             System.out.printf("Zip file %s extracted successfully inwards %s", FILE_NAME, OUTPUT_DIR);         } finally {             file.close();         }      }      /*      * Example of reading Zip file using ZipInputStream inwards Java.      */      private static void readUsingZipInputStream() throws IOException {         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(FILE_NAME));         finally ZipInputStream is = new ZipInputStream(bis);          try {             ZipEntry entry;             while ((entry = is.getNextEntry()) != null) {                 System.out.printf("File: %s Size %d  Modified on %TD %n", entry.getName(), entry.getSize(), new Date(entry.getTime()));                 extractEntry(entry, is);             }         } finally {             is.close();         }      }      /*      * Utility method to read  information from InputStream      */      private static void extractEntry(final ZipEntry entry, InputStream is) throws IOException {         String exractedFile = OUTPUT_DIR + entry.getName();         FileOutputStream fos = null;          try {             fos = new FileOutputStream(exractedFile);             finally byte[] buf = new byte[BUFFER_SIZE];             int read = 0;             int length;              while ((length = is.read(buf, 0, buf.length)) >= 0) {                 fos.write(buf, 0, length);             }          } catch (IOException ioex) {             fos.close();         }      }  }  Output: Iterating over zip file : C:\temp\pics.zip File: Image  (11).png Size 21294  Modified on 10/24/13 File: Image  (1).png Size 22296  Modified on 11/19/13 File: Image  (2).png Size 10458  Modified on 10/24/13 File: Image  (3).png Size 18425  Modified on 11/19/13 File: Image  (4).png Size 31888  Modified on 11/19/13 File: Image  (5).png Size 27454  Modified on 11/19/13 File: Image  (6).png Size 67608  Modified on 11/19/13 File: Image  (7).png Size 8659  Modified on 11/19/13 File: Image  (8).png Size 40015  Modified on 11/19/13 File: Image  (9).png Size 17062  Modified on 10/24/13 File: Image  (10).png Size 42467  Modified on 10/24/13 Zip file C:\temp\pics.zip extracted successfully in C:\temp\Images\

In club to run this file, brand your yous must have, zip file amongst refer pics.zip inwards C:\temp, as well as output directory C:\temp\Images available, otherwise it volition throw java.lang.NullPointerException. After successful run of this program, yous tin run across contents of zip file extracted within output directory. By the way, equally an exercise, yous tin enhance this plan to acquire refer of zip file from user as well as do output directory of same name.

That's all nearly How to read Zip file inwards Java. We receive got seen 2 dissimilar approaches to iterate over each file entries inwards Zip file as well as retrieve them. You should prefer using ZipFile over ZipInputStream for iterating over each file from archive. It's also expert to know that java.uti.zip packet also back upwards GZIP file formats, which agency yous tin also read .gz files generated past times gzip command inwards UNIX from your Java program.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

0 Response to "2 Examples To Read Cypher Files Inwards Java, Zipfile Vs Zipinputstream"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel