Mainly used in Zip:
1 Import java.util.Enumeration; 2 Import java.util.zip.CRC32; 3 Import Java.util.zip.CheckedOutputStream; 4 Import Java.util.zip.ZipEntry; 5 Import Java.util.zip.ZipFile; 6 import Java.util.zip.ZipOutputStream;
Unzip the file:
1 /**2 * Decompression function. Unzip the Zippath file into the FolderPath directory.3 *4 * @paramzipfile zip file5 * @paramDestDir extracted to the file path6 * @throwsjava.io.IOException7 */8 Public Static voidUpzipfile (File zipfile, String destDir) {9File Pathfile =NewFile (DestDir + "/");Ten if(!pathfile.exists ()) { One pathfile.mkdirs (); A } - - Try { theZipFile zip =NewZipFile (ZipFile); - - for(Enumeration<?> entries =zip.entries (); entries.hasmoreelements ();) { -ZipEntry entry =(ZipEntry) entries.nextelement (); +String Zipentryname =entry.getname (); - +InputStream in =Zip.getinputstream (entry); AString Outpath = (DestDir + "/" + Zipentryname). ReplaceAll ("\\*", "/"); at - //to determine if a path exists, create a file path if it does not exist -File File =NewFile (outpath.substring (0, Outpath.lastindexof ('/')))); - if(!file.exists ()) { - file.mkdirs (); - } in //determine whether the full path of the file is a folder, if it is uploaded above, do not need to unzip - if(NewFile (Outpath). Isdirectory ()) { to Continue; + } - //output file path information the System.out.println (outpath); * $OutputStream out =NewFileOutputStream (outpath);Panax Notoginseng byte[] Buf1 =New byte[1024]; - intLen; the while(len = In.read (buf1)) > 0) { +Out.write (BUF1, 0, Len); A } the in.close (); + out.close (); - } $}Catch(IOException e) { $ e.printstacktrace (); - } -}
Compressed files:
(1) First to declare a compression class:
1 Public Static classZipcompressor {2 Static Final intBUFFER = 1024 * 128;3 4 PrivateFile ZipFile;5 6 Publiczipcompressor (String pathName) {7ZipFile =NewFile (pathName);8 }9 Ten Public voidCompress (String srcpathname) { OneFile File =NewFile (srcpathname); A if(!file.exists ()) - Throw NewRuntimeException (Srcpathname + "does not exist! "); - Try { theFileOutputStream FileOutputStream =NewFileOutputStream (ZipFile); -Checkedoutputstream cos =NewCheckedoutputstream (FileOutputStream,NewCRC32 ()); -Zipoutputstream out =Newzipoutputstream (COS); -String Basedir = ""; + compress (file, out, basedir); - out.close (); +}Catch(Exception e) { A Throw NewRuntimeException (e); at } - } - - Private voidCompress (file file, zipoutputstream out, String basedir) { - /*determine whether a directory or a file*/ - if(File.isdirectory ()) { inSystem.out.println ("Compression:" + Basedir +file.getname ()); - This. compressdirectory (file, out, basedir); to}Else { +System.out.println ("Compression:" + Basedir +file.getname ()); - This. Compressfile (file, out, basedir); the } * } $ Panax Notoginseng /**Compress a directory*/ - Private voidcompressdirectory (File dir, zipoutputstream out, String basedir) { the if(!dir.exists ()) + return; A thefile[] Files =dir.listfiles (); + for(inti = 0; i < files.length; i++) { - /*Recursive*/ $Compress (Files[i], out, Basedir + dir.getname () + "/"); $ } - } - the /**Compress a file*/ - Private voidcompressfile (file file, zipoutputstream out, String basedir) {Wuyi if(!file.exists ()) { the return; - } Wu Try { -Bufferedinputstream bis =NewBufferedinputstream (Newfileinputstream (file)); AboutZipEntry entry =NewZipEntry (Basedir +file.getname ()); $ out.putnextentry (entry); - intcount; - byteData[] =New byte[BUFFER]; - while(count = bis.read (data, 0, BUFFER))! =-1) { AOut.write (data, 0, count); + } the bis.close (); -}Catch(Exception e) { $ Throw NewRuntimeException (e); the } the } the}
Compression classes use:
1 Public Static void Compress (String srcpathname, String zippathname) {2 New zipcompressor (zippathname); 3 zipcompresspr.compress (srcpathname); 4 }
Android decompression Feature