Recently in self-study javaweb, First review Java, return to the teacher's things to find back ( knowledge if not soon will forget Ah ).
Today I saw the zip compression, decided to tidy up a bit.
Java encapsulates the contents of zip compression in java.util.zip bao, using Java to implement ZIP compression, regardless of the compression algorithm, Java has already encapsulated these
Actually, using Java to implement ZIP compression involves the concept of an "input-output stream" .
Using Java to implement a zip compression of a file, the process can be simply expressed as:
Of course, the implementation is more complicated than this, such as to first write to the directory entry point like a zip file. If you want to compress the contents of a folder, you traverse the files and subfolders in the folder.
First give the source code, and then explain:
ImportJava.io.*;ImportJava.util.*;Importjava.util.zip.*;classzipcompress{PrivateString Zipfilename;//Destination zip file PrivateString sourceFileName;//source files (files or folders with compression) Publiczipcompress (String zipfilename,string sourcefilename) { This. zipfilename=Zipfilename; This. sourcefilename=sourceFileName; } Public voidZip ()throwsException {//file ZipFile = new file (zipfilename);System.out.println ("in compression ..."); //Create a zip output streamZipoutputstream out =NewZipoutputstream (NewFileOutputStream (zipfilename)); //creating buffered output streamsBufferedoutputstream BOS =NewBufferedoutputstream (out); File sourcefile=NewFile (sourceFileName); //calling FunctionsCompress (Out,bos,sourcefile,sourcefile.getname ()); Bos.close (); Out.close (); System.out.println ("Compression Complete"); } Public voidCompress (zipoutputstream out,bufferedoutputstream bos,file sourcefile,string Base)throwsException {//If the path is a directory (folder) if(Sourcefile.isdirectory ()) {//Remove files (or subfolders) from a folderfile[] Flist =Sourcefile.listfiles (); if(flist.length==0)//If the folder is empty, simply write a directory entry point in the destination zip file{System.out.println (base+"/"); Out.putnextentry (NewZipEntry (base+ "/") ); } Else//If the folder is not empty, recursively calls compress, and each file (or folder) in the folder is compressed { for(inti=0;i<flist.length;i++) {Compress (out,bos,flist[i],base+"/"+flist[i].getname ()); } } } Else//if it is not a directory (folder), which is a file, write the directory entry point first, and then write the file to the zip file{out.putnextentry (NewZipEntry (base)); FileInputStream Fos=NewFileInputStream (sourcefile); Bufferedinputstream bis=NewBufferedinputstream (FOS); inttag; System.out.println (base); //write the source file to a zip file while((Tag=bis.read ())!=-1) {bos.write (tag); } bis.close (); Fos.close (); } }} Public classTestzip { Public Static voidMain (string[] args) {zipcompress zipcom=NewZipcompress ("d:\\ movie. zip", "f:\\ movie"); Try{zipcom.zip (); } Catch(Exception e) {e.printstacktrace (); } }}
The Compress () method is called after the Zipoutputstream (Zip output stream) is first created in the Zip () method
In the Compress () method, you complete the compression of each file in the folder by recursion.
The specific:
First determine whether the input file is a folder or a file, if it is a file, direct compression, if the folder is to recursively call compress () to traverse the files in the compressed folder.
If it is a file, first write a directory entry point in the zip file (the so-called directory entry point, the personal understanding is the source file in the destination zip file path )
The source file is then read into memory with the file input stream FileInputStream, and the data read into the memory is written to the zip file using the Zipoutputstream (Zip output stream), which completes the compression. ( actually the input output stream )
When the folder is empty, just write a directory entry point (note that you must add a "\" in the end to indicate a directory).
The result of the operation is:
Source file:
Where folder 1 contains two files, folder 2 is empty.
The compressed file is:
The directory entry point of the empty folder mentioned above must be added "\" to indicate a folder, not a file, we can remove the "\" in the program, the result is:
You can see the folder 2 that was originally a folder, programming the file. So the directory entry point can be understood as the source file in the zip file corresponding to the path, the relative path should be consistent with the relative path in the source folder, of course, can also be inconsistent, for example, we add a string "I love" before base,
Out.putnextentry (New ZipEntry ("I Love" +base));
The result is:
You can see that the root directory in the zip becomes "I love movies" rather than the original movie.
Level Limited ... Please give me a lot of advice ....
Java zip compression implementation