2 Examples To Read Cipher Files Inward Java, Zipfile Vs Zipinputstream

ZIP format is i of the most pop compression machinery inwards figurer world. Influenza A virus subtype H5N1 Zip file may contains multiples files or folder inwards compressed format.  Java API provides extensive back upward 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 in addition to display what entries it contains, in addition to and thus extract them inwards a folder. In this tutorial nosotros volition larn how to do this chore inwards Java. There are ii ways you lot tin iterate over all items inwards a given zip archive, you lot 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 position out of bytes. Which way you lot 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 in addition to UTF-8 charset is used to decode the entry names in addition to comments.

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

It has to read in addition to decompress all zip information inwards gild to accomplish EOF for each entry in addition to read header of side past times side entry. That's why its meliorate to role ZipFile class over ZipInputStream for iterating over all entries from archive.

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



Reading Zip archive inwards Java

In this example, I conduct maintain 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 in addition to modified engagement in addition to time.


You tin inquire ZipFile for InputStream corresponding to this file entry for extracting existent data. Which means, you lot exclusively incur terms of decompression, when you lot truly require to. By using java.util.zip.ZipFile, you lot tin banking concern check each of entry in addition to exclusively extract for certain entries, depending upon your logic.

ZipFile is skillful for both sequential in addition to random access of private file entries. On the other hand, if you lot are using ZipInptStream in addition to thus similar any other InputStream, you lot volition require to procedure all entries sequentially, equally shown inwards minute example.

Key betoken to remember, specially if you lot are processing large zip archives is that, Java half-dozen exclusively back upward zip file upward to 2GB. Thankfully Java seven supports zip64 mode, which tin move used to procedure large zip file amongst size to a greater extent than than 2GB.

 ZIP format is i of the most pop compression machinery inwards figurer earth 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 programme to iterate in addition to read file entries from Zip archive.  * This programme demonstrate ii ways to retrieve files from Zip using ZipFile in addition to past times using ZipInputStream class.  * @author Javin  */  public class ZipFileReader {      // This Zip file contains xi PNG images     private static in conclusion String FILE_NAME = "C:\\temp\\pics.zip";     private static in conclusion String OUTPUT_DIR = "C:\\temp\\Images\\";     private static in conclusion 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 shape      */      private static void readUsingZipFile() throws IOException {         in conclusion ZipFile file = new ZipFile(FILE_NAME);         System.out.println("Iterating over zip file : " + FILE_NAME);          try {             in conclusion Enumeration<? extends ZipEntry> entries = file.entries();             while (entries.hasMoreElements()) {                 in conclusion 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));         in conclusion 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);             in conclusion 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 gild to run this file, brand your you lot must have, zip file amongst cite pics.zip inwards C:\temp, in addition to output directory C:\temp\Images available, otherwise it volition throw java.lang.NullPointerException. After successful run of this program, you lot tin run across contents of zip file extracted within output directory. By the way, equally an exercise, you lot tin enhance this programme to acquire cite of zip file from user in addition to do output directory of same name.

That's all virtually How to read Zip file inwards Java. We conduct maintain seen ii dissimilar approaches to iterate over each file entries inwards Zip file in addition to retrieve them. You should prefer using ZipFile over ZipInputStream for iterating over each file from archive. It's also skillful to know that java.uti.zip parcel also back upward GZIP file formats, which way you lot 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!


Sumber https://javarevisited.blogspot.com/

0 Response to "2 Examples To Read Cipher Files Inward Java, Zipfile Vs Zipinputstream"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel