Android Zip Compression Tool class

Source: Internet
Author: User
Tags locale parent directory zip
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;
 }
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.