[Java iO] _ compressing stream notes

Source: Internet
Author: User

[Java iO] _ compressing stream notes

Objectives of this chapter:
Measure the test taker's understanding about the main functions of the compressed stream.
Understand three compression formats supported by Java Io
Understanding the functions of zipoutputstream, zipfile, and zipinputstream

3. Details

Zipentry

Zipoutputstream
Compresses a file or folder into a zip file.

Instance code:

Import Java. io. file; import Java. io. fileinputstream; import Java. io. inputstream; import java.util.zip. zipentry; import java.util.zip. zipinputstream; import Java. io. fileinputstream; public class zipinputstreamdemo01 {public static void main (string ARGs []) throws exception {// All exceptions Throw file zipfile = new file ("D:" + file. separator + "mldn.zip"); // defines the compressed file name zipinputstream input = NULL; // defines the compressed input stream input = new zipinputstream (New fileinputstream (zipfile )); // instantiate zipinputstream zipentry entry = input. getnextentry (); // obtain a compressed object system. out. println ("compressed Object Name:" + entry. getname (); input. close ();}};

The above operation code only compresses a file. What if I want to complete a folder?

Import Java. io. file; import Java. io. outputstream; import Java. io. inputstream; import java.util.zip. zipentry; import java.util.zip. zipfile; import java.util.zip. zipinputstream; import Java. io. fileinputstream; import Java. io. fileoutputstream; public class zipinputstreamdemo02 {public static void main (string ARGs []) throws exception {// All exceptions Throw file = new file ("D:" + file. separator + "mldndir.zip" ); // Define the compressed file name file OUTFILE = NULL; // The zipfile = new zipfile (File) folder operation is required for the output file ); // instantiate the zipfile object zipinputstream zipinput = NULL; // defines the compressed input stream outputstream out = NULL; // defines the output stream, which is used to output inputstream input = NULL for each object content; // define the input stream and read every zipentry entry = NULL; // each compressed object zipinput = new zipinputstream (New fileinputstream (File )); // instantiate zipinputstream while (Entry = zipinput. getnexten Try ())! = NULL) {// obtain a compressed object system. Out. println ("decompress" + entry. getname () +. "); OUTFILE = new file (" D: "+ file. Separator + entry. getname (); // defines the output file path if (! OUTFILE. getparentfile (). exists () {// If the Output Folder does not exist OUTFILE. getparentfile (). mkdir (); // create a folder} If (! OUTFILE. exists () {// determine whether the output file exists. createnewfile (); // create a file} input = zipfile. getinputstream (entry); // get the input stream of each object out = new fileoutputstream (OUTFILE); // instantiate the file output stream int temp = 0; while (temp = input. read ())! =-1) {out. write (temp);} input. close (); // close the input stream out. close (); // close the output stream} input. close ();}};

3.2. zipfile
Is a class specifically used to indicate compressed files

Zipfile must receive an instance of the file class during instantiation. the instance of this file class points to a compressed *. ZIP file.

The file class must be referenced during instantiation.

Import Java. io. file; import Java. io. fileinputstream; import Java. io. inputstream; import java.util.zip. zipentry; import java.util.zip. zipoutputstream; import java.util.zip. zipfile; import Java. io. fileoutputstream; public class zipfiledemo01 {public static void main (string ARGs []) throws exception {// All exceptions Throw file = new file ("D:" + file. separator + "mldn.zip"); // find the compressed file zipfile = new zipfile (File); // instantiate the zipfile object system. out. println ("compressed file name:" + zipfile. getname (); // get the name of the compressed file }};

Use zipfilefiles to decompress mldn.zip.

Import Java. io. file; import Java. io. fileinputstream; import Java. io. inputstream; import Java. io. outputstream; import java.util.zip. zipentry; import java.util.zip. zipoutputstream; import java.util.zip. zipfile; import Java. io. fileoutputstream; public class zipfiledemo02 {public static void main (string ARGs []) throws exception {// All exceptions Throw file = new file ("D:" + file. separator + "mldn.zip"); // locate Compressed file outputfile = new file ("D:" + file. separator + "mldn_unzip.txt"); // defines the name of the extracted file zipfile = new zipfile (File); // instantiate the zipfile object zipentry entry = zipfile. getentry ("mldn.txt"); // get a compressed object outputstream out = new fileoutputstream (outputfile); // instantiate the output stream inputstream input = zipfile. getinputstream (entry); // get the input stream int temp = 0 for a compressed object; while (temp = input. read ())! =-1) {out. Write (temp);} input. Close (); // close the input stream out. Close (); // close the output stream }};

3.3. zipinputstream
You can obtain each zipentry object without entering the object name.

Import Java. io. file; import Java. io. fileinputstream; import Java. io. inputstream; import java.util.zip. zipentry; import java.util.zip. zipoutputstream; import Java. io. fileoutputstream; public class zipoutputstreamdemo01 {public static void main (string ARGs []) throws exception {// All exceptions Throw file = new file ("D:" + file. separator + "mldn.txt"); // defines the file to be compressed. zipfile = new file ("D:" + file. separato R + "mldn.zip"); // defines the compressed file name inputstream input = new fileinputstream (File); // defines the input stream zipoutputstream zipout = NULL; // declare the compressed stream object zipout = new zipoutputstream (New fileoutputstream (zipfile); zipout. putnextentry (New zipentry (file. getname (); // set zipentry object zipout. setcomment ("www.mldnjava.cn"); // set the annotation int temp = 0; while (temp = input. read ())! =-1) {// read the zipout content. write (temp); // compressed output} input. close (); // close the input stream zipout. close (); // close the output stream }};

With this feature and zipfile, You can decompress the folder.
Zipfile object, you can find the input stream of each zipentry, but zipinputstream does not get every input stream, so zipfile is required, but zipinputstream does not need the name of each zipentry when obtaining each zipentry.

Import Java. io. file; import Java. io. fileinputstream; import Java. io. inputstream; import java.util.zip. zipentry; import java.util.zip. zipoutputstream; import Java. io. fileoutputstream; public class zipoutputstreamdemo02 {public static void main (string ARGs []) throws exception {// All exceptions Throw file = new file ("D:" + file. separator + "mldn"); // defines the file zipfile = new file ("D:" + file. separator + "Mldndir.zip"); // defines the compressed file name inputstream input = NULL; // defines the file input stream zipoutputstream zipout = NULL; // declare the compressed stream object zipout = new zipoutputstream (New fileoutputstream (zipfile); zipout. setcomment ("www.mldnjava.cn"); // set the annotation int temp = 0; If (file. isdirectory () {// determine whether the folder is file lists [] = file. listfiles (); // list all files for (INT I = 0; I <lists. length; I ++) {input = new fileinputstream (lists [I]); // defines the input stream of the file Zipout. putnextentry (New zipentry (file. getname () + file. separator + lists [I]. getname (); // set the zipentry object while (temp = input. read ())! =-1) {// read the zipout content. write (temp); // compressed output} input. close (); // close the input stream} zipout. close (); // close the output stream }};
Related Article

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.