Java with zip for multiple file save

Source: Internet
Author: User
Tags command line crc32 zip

The

Java 1.1 Library with ZIP support appears more comprehensive. It makes it easy to save multiple files. There is even a separate class to simplify the read operation of the zip file. This library is a standard zip format, so it works well with the large number of compression and decompression tools currently available on the Internet. The following example takes the same form as the previous one, but can control any number of command-line arguments according to our needs. In addition, it shows how to use the Checksum class to compute and validate the file's checksum (Checksum). Two types of Checksum:adler32 can be selected (faster) and CRC32 (slower but more accurate).
 

: Zipcompress.java//Uses Java 1.1 Zip compression to compress/No. of files whose names are//on the
Command line.
Import java.io.*;
Import java.util.*;

Import java.util.zip.*; public class Zipcompress {public static void main (string[] args) {try {FileOutputStream f = new Fil
      Eoutputstream ("Test.zip");
      Checkedoutputstream csum = new Checkedoutputstream (f, New Adler32 ());
      Zipoutputstream out = new Zipoutputstream (new Bufferedoutputstream (csum));
      Out.setcomment ("A test of Java zipping");
          Can ' t read the above comment, though for (int i = 0; i < args.length; i++) {System.out.println (
        "Writing file" + args[i]);
        BufferedReader in = new BufferedReader (new FileReader (args[i));
        Out.putnextentry (New ZipEntry (args[i));
        int C;
        while ((c = in.read ())!=-1) out.write (c);
      In.close (); }
      Out.close ();
      Checksum valid only after the file//has been closed!
      System.out.println ("Checksum:" + csum.getchecksum (). GetValue ());
      Now extract the Files:System.out.println ("Reading file");
      FileInputStream fi = new FileInputStream ("Test.zip");
      Checkedinputstream Csumi = new Checkedinputstream (FI, New Adler32 ());
      Zipinputstream in2 = new Zipinputstream (new Bufferedinputstream (Csumi));
      ZipEntry ze;
      System.out.println ("Checksum:" + csumi.getchecksum (). GetValue ());
        while (ze = in2.getnextentry ())!= null) {System.out.println ("Reading file" + ze);
        int x;
      while ((x = In2.read ())!=-1) System.out.write (x);
      } in2.close ();
      Alternative way to open and read//zip files:zipfile ZF = new ZipFile ("Test.zip");
      Enumeration E = Zf.entries ();
       while (E.hasmoreelements ()) { ZipEntry ze2 = (zipentry) e.nextelement ();
        System.out.println ("File:" + ze2);
    ... and extract the "Data as before}" catch (Exception e) {e.printstacktrace (); }
  }
} ///:~

For each file that you want to include in the archive, you must call Putnextentry () and pass it to a ZipEntry object. The ZipEntry object contains a full-featured interface that allows you to get and set all the data that is acceptable on that particular entry (portal) in the zip file: name, compression and compression length, date, CRC checksum, data for extra fields, comments, Compression method and whether it is a directory entry, and so on. However, although the ZIP format provides a way to set a password, the Java Zip Library does not provide this support. And although Checkedinputstream and Checkedoutputstream provide support for Adler32 and CRC32 checksums at the same time, ZipEntry only supports CRC-enabled interfaces. This is a limitation of the base zip format, but it limits our use of faster Adler32.
To extract files, Zipinputstream provides a getnextentry () method that can return to the next zipentry on some premises. As a more concise method, you can read the file with the ZipFile object. The object has a entries () method that can return a enumeration (enumeration) for ZipEntry.
To read checksums, you must have a number of access rights to the associated checksum object. A handle to the Checkedoutputstream and Checkedinputstream objects is preserved here. However, you can also only occupy a handle to a checksum object.
One puzzling method in the zip stream is setcomment (). As shown earlier, we can set the annotation content when we write a file, but there is no way to remove the annotation within the zipinputstream. It seems that full support for annotations can only be provided on a zipentry-by-portal basis.
Of course, using gzip or zip libraries is not limited to files-you can compress anything, including data to be sent over a network connection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.