Package com.chy.utils;
Import Java.io.BufferedInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.Locale;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipInputStream;
Import Java.util.zip.ZipOutputStream;
Import Android.text.TextUtils;
/** * Zip Compression tool */public class Ziputils {private static final int buff_size = 1024; /** * @param zos * Compressed stream * @param parentdirname * Parent Directory * @param file * to be compressed * @param buffer * *url:http://www.bianceng.cn/os/extra/201609/50420.htm * @return stop and return whenever a file in the directory fails to compress Back */private static Boolean ZipFile (Zipoutputstream Zos, string parentdirname, File file, byte[] buffer) {String zip
FilePath = Parentdirname + file.getname ();
if (File.isdirectory ()) {Zipfilepath + = File.separator; For (File f:file.listfiles ()) {if!zipfile (Zos, ZIPFILepath, F, buffer) {return false;
} return true;
else {try {Bufferedinputstream bis = new Bufferedinputstream (new FileInputStream (file));
ZipEntry zipentry = new ZipEntry (Zipfilepath);
Zipentry.setsize (File.length ());
Zos.putnextentry (ZipEntry);
while (bis.read (buffer)!=-1) {zos.write (buffer);
} bis.close ();
return true;
catch (FileNotFoundException ex) {ex.printstacktrace ();
catch (IOException ex) {ex.printstacktrace ();
return false; }/** * @param srcpath * Compressed file or directory * @param dstpath zip file * @return As long as the file to be compressed has a Compression fails to stop compression and return (equivalent to compression directly on Windows)/public static Boolean ZipFile (String Srcpath, String dstpath) {File srcfile = new
File (Srcpath);
if (!srcfile.exists ()) {return false;
} byte[] buffer = new Byte[buff_size];
try {zipoutputstream Zos = new Zipoutputstream (new FileOutputStream (Dstpath)); Boolean result = ZipFile (Zos, "", srcfile, buffer);
Zos.close ();
return result;
catch (FileNotFoundException ex) {ex.printstacktrace ();
catch (IOException ex) {ex.printstacktrace ();
return false; /** * @param srcpath * Uncompressed zip file * @param dstpath * Zip uncompressed directory * @return as long as there is an error in the decompression process , stop and return immediately (equivalent to immediate decompression on Windows)/public static Boolean Unzipfile (String Srcpath, String dstpath) {if textutils.isempt Y (srcpath) | |
Textutils.isempty (Dstpath)) {return false;
} File Srcfile = new file (Srcpath);
if (!srcfile.exists () | | |!srcfile.getname (). toLowerCase (Locale.getdefault ()). EndsWith ("Zip")) {return false;
} File Dstfile = new file (Dstpath);
if (!dstfile.exists () | | |!dstfile.isdirectory ()) {dstfile.mkdirs ();
try {Zipinputstream ZiS = new Zipinputstream (new FileInputStream (srcfile));
Bufferedinputstream bis = new Bufferedinputstream (ZIS);
ZipEntry zipentry = null;
byte[] buffer = new Byte[buff_size]; if (!DSTPATH.ENdswith (File.separator)) {Dstpath + = File.separator;
while ((ZipEntry = Zis.getnextentry ())!= null) {String FileName = Dstpath + zipentry.getname ();
File File = new file (fileName);
File Parentdir = File.getparentfile ();
if (!parentdir.exists ()) {parentdir.mkdirs ();
FileOutputStream fos = new FileOutputStream (file);
while (bis.read (buffer)!=-1) {fos.write (buffer);
} fos.close ();
} bis.close ();
Zis.close ();
return true;
catch (FileNotFoundException ex) {ex.printstacktrace ();
catch (IOException ex) {ex.printstacktrace ();
return false;
}
}