The Java.util.zip package provides classes that can handle the compression and decompression of files, which inherit from the byte stream class Outputsteam and InputStream. Gzipoutputstream and Zipoutputstream can compress data into GZIP and ZIP format respectively, Gzipinpputstream and Zipinputstream can restore the compressed data.
The general steps to write a file to a compressed file are as follows:
- Generates a compressed class object that is associated with the compressed file to be generated.
- A compressed file usually contains more than one file, and each file to be added is called a compressed portal, and a compressed entry object is generated using ZipEntry (String FileName).
- Use Putnextentry (ZipEntry entry) to add the compressed entry to the compressed file.
- Writes the contents of the file to this compressed file.
- Use Closeentry () to end the current compression entry and continue to the next compressed entry.
The general steps to read a file from a compressed file are as follows:
- Generates a compressed class object that is associated with the compressed file to be read into.
- Use Getnextentry () to get the next compressed entry.
1"Example 10-13"Enter several file names, compress all the files to" Ep10_13.zip ", and unzip and display them from the compressed file. 2 //********** Ep10_13.java **********3 ImportJava.io.*;4 ImportJava.util.*;5 Importjava.util.zip.*;6 classep10_13{7 Public Static voidMain (String args[])throwsioexception{8FileOutputStream a=NewFileOutputStream ("Ep10_13.zip");9 //working with compressed filesTenZipoutputstream out=NewZipoutputstream (NewBufferedoutputstream (a)); One for(inti=0;i<args.length;i++) {//process each file entered on the command line ASystem.out.println ("Writing file" +args[i]); -Bufferedinputstream in=NewBufferedinputstream (NewFileInputStream (Args[i])); -Out.putnextentry (NewZipEntry (Args[i]));//set ZipEntry Object the intb; - while((B=in.read ())!=-1) -Out.write (b);//read from source file, write to compressed file - in.close (); + } - out.close (); + //unzip the file and display ASystem.out.println ("Reading file"); atFileInputStream d=NewFileInputStream ("Ep10_13.zip"); -Zipinputstream inout=NewZipinputstream (NewBufferedinputstream (d)); - ZipEntry Z; - - while((Z=inout.getnextentry ())! =NULL){//Get the entrance -System.out.println ("Reading file" +z.getname ());//Show file initial name in intx; - while((X=inout.read ())!=-1) to System.out.write (x); + System.out.println (); - } the inout.close (); * } $}
Example 10-13 after running, in the program directory to establish a Ep10_13.zip compressed file, using the decompression software (such as WinRAR, etc.), you can open it. At the command prompt, the program runs as shown in result 10-12:
Figure 10-12 Example 10_13 running results
Series Articles:Java know how much (top)Java know how much (interface) interfaceJava knows how much (40) the difference between an interface and an abstract classJava know how much (41) generic explanationJava know how much (42) the range of generic wildcard characters and type parametersJava know how much (43) Exception Handling BasicsJava know how much (44) exception typeJava know how much (45) uncaught ExceptionsHow much Java knows (the) use of try and catchJava know how much (47) use of multiple catch statementsJava knows how much (in) the nesting of try statementsJava know how much (a) Throw: Exception throwsJava know how many () Java throws clausesJava knows how many (or) finallyJava know how much (52) built-in exceptionsJava know how much (53) Use Java to create your own exception subclassesJava know how much (54) assertion detailedJava know how many (55) threadsJava know how much (56) threading ModelJava know how much (57) main threadJava know how much (58) thread Runnable interface and thread class explanationJava know how much (59) Create multithreadingJava know how much (isAlive) and join () useJava know how much (61) thread PriorityJava know how much (62) thread synchronizationJava know how much (63) inter-thread communicationJava know how much (64) thread deadlockJava know how much (65) thread hangs, resumes, and terminatesJava know how much (66) Overview of input and output (IO) and streamsJava know how much (67) character-oriented input streamJava know how much (68) character-oriented output streamJava know how much (69) byte-oriented input and output streamJava know how much (70) application for byte streamJava know how much (71) file and directory managementJava know how much (72) random Read and write files
Java know how much (73) file compression processing