Android Zip and AndroidZip

Source: Internet
Author: User

Android Zip and AndroidZip

Import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. io. unsupportedEncodingException; import java. util. arrayList; import java. util. collection; import java. util. enumeration; Import java.util.zip. zipEntry; import java.util.zip. zipException; import java.util.zip. zipFile; import java.util.zip. zipOutputStream;/*** Android Zip compression and decompression */public class Unzip {private static final int BUFF_SIZE = 1024*1024; // 1 M Byte/*** batch compressed file (folder) ** @ param resFileList file to be compressed (folder) list * @ param zipFile: the compressed File generated * @ throws IOException throws */public static void zipFiles (Collection <File> resFile List, File zipFile) throws IOException {ZipOutputStream zipout = new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (zipFile), BUFF_SIZE); for (File resFile: resFileList) {zipFile (resFile, zipout, "");} zipout. close ();}/*** batch compressed file (folder) ** @ param resFileList file to be compressed (folder) list * @ param zipFile generated compressed file * @ param comment comments of compressed file * @ throws IOException thrown when the compression process fails */public static void zip Files (Collection <File> resFileList, File zipFile, String comment) throws IOException {ZipOutputStream zipout = new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (zipFile), BUFF_SIZE )); for (File resFile: resFileList) {zipFile (resFile, zipout, "");} zipout. setComment (comment); zipout. close ();}/*** decompress a file ** @ param zipFile compressed file * @ param folderPath decompressed target directory * @ param isDelete * @ Throws IOException: When an error occurs during decompression, throw */public static void upZipFile (File zipFile, String folderPath, boolean isDelete) {String strZipName = zipFile. getName (); folderPath + = "/" + strZipName. substring (0, strZipName. lastIndexOf (". "); File desDir = new File (folderPath); if (! DesDir. exists () {desDir. mkdirs ();} ZipFile zf; try {zf = new ZipFile (zipFile); for (Enumeration <?> Entries = zf. entries (); entries. hasMoreElements ();) {ZipEntry entry = (ZipEntry) entries. nextElement (); if (entry. isDirectory () {String dirstr = entry. getName (); dirstr = new String (dirstr. getBytes ("8859_1"), "GB2312"); File f = new File (dirstr); f. mkdir (); continue;} InputStream in = zf. getInputStream (entry); String str = folderPath + File. separator + entry. getName (); str = new String (str. getBy Tes ("8859_1"), "GB2312"); File desFile = new File (str); if (! DesFile. exists () {File fileParentDir = desFile. getParentFile (); if (! FileParentDir. exists () {fileParentDir. mkdirs ();} desFile. createNewFile ();} OutputStream out = new FileOutputStream (desFile); byte buffer [] = new byte [BUFF_SIZE]; int realLength; while (realLength = in. read (buffer)> 0) {out. write (buffer, 0, realLength);} in. close (); out. close () ;}if (isDelete) {zipFile. delete () ;}} catch (ZipException e) {e. printStackTrace ();} catch (IOException e) {e. printSt AckTrace ();}} /*** extract the file name containing the input text ** @ param zipFile compressed file * @ param folderPath destination folder * @ param nameContains imported file matching name * @ throws ZipException compression format * @ throws IOException IO error thrown */public static ArrayList <File> upZipSelectedFile (File zipFile, string folderPath, String nameContains) throws ZipException, IOException {ArrayList <File> fileList = new ArrayList <File> (); File desDir = new File (folderP Ath); if (! DesDir. exists () {desDir. mkdir ();} ZipFile zf = new ZipFile (zipFile); for (Enumeration <?> Entries = zf. entries (); entries. hasMoreElements ();) {ZipEntry entry = (ZipEntry) entries. nextElement (); if (entry. getName (). contains (nameContains) {InputStream in = zf. getInputStream (entry); String str = folderPath + File. separator + entry. getName (); str = new String (str. getBytes ("8859_1"), "GB2312"); // str. getBytes ("GB2312"), "8859_1" output // str. getBytes ("8859_1"), "GB2312" input File desFile = ne W File (str); if (! DesFile. exists () {File fileParentDir = desFile. getParentFile (); if (! FileParentDir. exists () {fileParentDir. mkdirs ();} desFile. createNewFile ();} OutputStream out = new FileOutputStream (desFile); byte buffer [] = new byte [BUFF_SIZE]; int realLength; while (realLength = in. read (buffer)> 0) {out. write (buffer, 0, realLength);} in. close (); out. close (); fileList. add (desFile) ;}} return fileList;}/*** get the compressed file list ** @ param zipFile compressed file ** @ return name of the compressed file * @ th Rows ZipException is thrown when the compression File format is incorrect * @ throws IOException is thrown when the decompression process fails */public static ArrayList <String> getEntriesNames (File zipFile) throws ZipException, IOException {ArrayList <String> entryNames = new ArrayList <String> (); Enumeration <?> Entries = getEntriesEnumeration (zipFile); while (entries. hasMoreElements () {ZipEntry entry = (ZipEntry) entries. nextElement (); entryNames. add (new String (getEntryName (entry ). getBytes ("GB2312"), "8859_1");} return entryNames ;} /*** obtain the compressed file object in the compressed file to obtain its attributes ** @ param zipFile compressed file * @ return returns a compressed file list * @ throws ZipException thrown when the compressed file format is incorrect * @ throws IOException when the IO operation is incorrect */public static Enumeration <?> GetEntriesEnumeration (File zipFile) throws ZipException, IOException {ZipFile zf = new ZipFile (zipFile); return zf. entries ();} /*** get the annotation of the compressed file object ** @ param entry compresses the file object * @ return the annotation of the compressed file object * @ throws UnsupportedEncodingException */public static String getEntryComment (ZipEntry entry) throws UnsupportedEncodingException {return new String (entry. getComment (). getBytes ("GB2312"), "8859_1 ");}/*** Get the name of the compressed file object ** @ param entry ZIP file object * @ return zip file object name * @ throws UnsupportedEncodingException */public static String getEntryName (ZipEntry entry) throws UnsupportedEncodingException {return new String (entry. getName (). getBytes ("GB2312"), "8859_1");}/*** compressed file ** @ param resFile the file to be compressed (folder) * @ param zipout: the target file to be compressed * @ param rootpath: the compressed file path * @ throws FileNotFoundException throws * @ throws IOExc Eption throws */private static void zipFile (File resFile, ZipOutputStream zipout, String rootpath) throws FileNotFoundException, IOException {rootpath = rootpath + (rootpath. trim (). length () = 0? "": File. separator) + resFile. getName (); rootpath = new String (rootpath. getBytes ("8859_1"), "GB2312"); if (resFile. isDirectory () {File [] fileList = resFile. listFiles (); for (File file: fileList) {zipFile (file, zipout, rootpath) ;}} else {byte buffer [] = new byte [BUFF_SIZE]; bufferedInputStream in = new BufferedInputStream (new FileInputStream (resFile), BUFF_SIZE); zipout. putNextEntry (new Zip Entry (rootpath); int realLength; while (realLength = in. read (buffer ))! =-1) {zipout. write (buffer, 0, realLength);} in. close (); zipout. flush (); zipout. closeEntry ();}}}


Decompress the zip file for android

Permission problems? Did you forget to register the permission for the SD card operation?
 
Download a Zip file from Http in Android and decompress it to SDCard

You can search and parse the zip file code on the Internet to read the server's zip file in the form of a stream and directly resolve it to the SD card.

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.