an overview1. Directory entry point
The directory entry point is the file's mapping in the compressed file, which represents the compressed file. When a file is compressed, a directory entry point is created and the file is written to the entry point of the directory. When extracting, gets the directory entry point and writes the contents of that directory entry point to the hard disk specified file.
If the directory entry point is a folder, it must be named with the path delimiter ending, and the name delimiter in the window operating system is "/".
2. Automatic creation of files
Whether you call CreateNewFile () to create a file, or if the output stream is responsible for creating the file when the output stream is created, you must ensure that the parent path already exists or the file cannot be created.
3. Creation of a directory
- Mkdirs (): Create a multilevel directory.
- mkdir (): Creates a first-level directory and fails to create if the parent path does not exist.
two compression1. Create a directory entry point
First create a directory entry point for the file or folder, the name of the directory entry point is the path of the current file relative to the compressed file, the directory entry point name of the folder must end with the name delimiter to distinguish it from the file.
New ZipEntry (compressed folder name + file.separator+ file or folder path);
2. Write to the directory entry point
Use the Zipoutputstream output stream to write the file to the corresponding directory entry point, write completion, and close the directory entry point.
3.Demo
PackageCom.javase.io;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipInputStream;ImportJava.util.zip.ZipOutputStream; Public classZiputils {/*** Compress a folder * *@throwsIOException*/ Public voidZipdirectory (String Path)throwsIOException {File file=NewFile (path); String Parent=file.getparent (); File ZipFile=NewFile (parent, File.getname () + ". Zip"); Zipoutputstream Zos=NewZipoutputstream (NewFileOutputStream (ZipFile)); Zip (zos, file, File.getname ()); Zos.flush (); Zos.close (); } /** * * @paramZos * Compressed output stream *@paramfile * Files currently required for compression *@parampath * Paths to the current file relative to the compressed folder *@throwsIOException*/ Private voidZip (zipoutputstream zos, file file, String path)throwsIOException {//first determine whether the file, or folder, files directly to the directory entry point, the folder is traversed if(File.isdirectory ()) {ZipEntry entry=NewZipEntry (path + file.separator);//The directory entry point for the folder must end with the name delimiterzos.putnextentry (entry); file[] Files=File.listfiles (); for(File x:files) {zip (Zos, x, Path+ File.separator +x.getname ()); } } Else{FileInputStream fis=NewFileInputStream (file);//the name of the directory entry point is the path to the file in the compressed fileZipEntry entry =Newzipentry (path); Zos.putnextentry (entry);//Create a directory entry point intLen = 0; byte[] buf =New byte[1024]; while(len = Fis.read (BUF))! =-1) {zos.write (buf,0, Len); } zos.flush (); Fis.close (); Zos.closeentry ();//Close the current directory entry point and move the input stream to the next directory entry point } }}three unzip the file1. Basic Ideas
When extracting the file, get the directory entry point in the compressed file, create the file object according to the directory entry point, and write the contents of the directory entry point to the hard disk file.
2.Demo
PackageCom.javase.io;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipInputStream;ImportJava.util.zip.ZipOutputStream; Public classZiputils {PrivateString BasePath; /*** Unzip the file * *@paramUnzip *@throwsIOException*/ Public voidUnzip (String unzip)throwsIOException {File file=NewFile (unzip); BasePath=file.getparent (); FileInputStream FIS=Newfileinputstream (file); Zipinputstream ZiS=NewZipinputstream (FIS); Unzip (ZIS); } Private voidUnzip (Zipinputstream ZiS)throwsIOException {zipentry entry=Zis.getnextentry (); if(Entry! =NULL) {File file=NewFile (BasePath + file.separator +entry.getname ()); if(File.isdirectory ()) {//There may be an empty folder if(!file.exists ()) file.mkdirs (); Unzip (ZIS); } Else{File Parentfile=File.getparentfile (); if(Parentfile! =NULL&&!parentfile.exists ()) parentfile.mkdirs (); FileOutputStream Fos=NewFileOutputStream (file);//The output stream must ensure that the parent path exists when creating the file intLen = 0; byte[] buf =New byte[1024]; while(len = Zis.read (BUF))! =-1) {fos.write (buf,0, Len); } fos.flush (); Fos.close (); Zis.closeentry (); Unzip (ZIS); } } }}
Java Compression and decompression