Zip Decompression in Android

Source: Internet
Author: User

Tag:android    compression    zip    decompression    

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 decompression */public class Unzip {private static Final int buff_size = 1024 * 1024; 1M Byte/** * Bulk Compressed file (clip) * * @param resfilelist file to compress (clip) list * @param zipfile generated compressed file * @throws I Oexception thrown when the compression process is faulted */public static void Zipfiles (Collection<file> resfilelist, File zipfile) throws Ioexc                eption {Zipoutputstream zipout = new Zipoutputstream (New Bufferedoutputstream ( ZipFile), buff_size));        for (File resfile:resfilelist) {zipfile (resfile, ZipOut, "");    } zipout.close (); }/** * Bulk compressed file (clip) * * @param resfilelist files to be Compressed (clip) list * @param zipfile generated compressed file * @param Comment Compressed text Note * @throws IOException when the compression process is faulted */public static void Zipfiles (collection<file> resfilelist, File Z Ipfile, String comment) throws IOException {Zipoutputstream zipout = new Zipoutputstream (New Bufferedou        Tputstream (New FileOutputStream (ZipFile), buff_size));        for (File resfile:resfilelist) {zipfile (resfile, ZipOut, "");        } zipout.setcomment (comment);    Zipout.close (); /** * Unzip a file * * @param zipfile Compressed file * @param folderpath extracted target directory * @param isdelete * @thr         oWS IOException Thrown when the decompression process is faulted */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.hasmoreelemen TS ();)            {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.getbytes ("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.printstacktrace ();} /** * Unzip the file name containing the incoming text files * * @param zipfile Compressed Files * @param folderpath destination folder * @param namecontains incoming  File Match name * @throws zipexception compression format thrown * @throws IOException IO Error when 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 (FolderPath);        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 = n                EW 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 a list of files within a compressed file * * @param zipfile Compressed file * @return file name in compressed file * @throws zipexception Compressed file format is incorrect when thrown * @throws IOException Thrown when an error occurs during decompression process */public static arraylist<string> Getentriesnames (File zipfile) throw        S 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; /** * Get compressed file inside compressed file object to get its properties * * @param zipfile Compressed file * @return return a list of compressed files * @throws zipexception compression Thrown when file format is incorrect * @throws ioexception IO operation is incorrect when thrown */public static enumeration<?> getentriesenumeration (file ZipF        Ile) throws Zipexception, IOException {zipfile ZF = new ZipFile (ZipFile);     return Zf.entries (); /** * Get comments on compressed file objects * * @param entry Compressed File object * @return Compressed file Object comments * @throws UNSUPPORTEDENCODINGEXCEP tion */public static String Getentrycomment (ZipEntry entry) throws Unsupportedencodingexception {return NE    W String (Entry.getcomment (). GetBytes ("GB2312"), "8859_1"); /** * Gets the name of the compressed file object * * @param entry Compressed File object * @return The name of the compressed file object * @throws unsupportedencodingexcep tion */public static STRing Getentryname (zipentry entry) throws Unsupportedencodingexception {return new String (Entry.getname (). GetBytes ("    GB2312 ")," 8859_1 ");  }/** * Compressed file * * @param resfile Required compressed file (clip) * @param zipout Compressed file * @param rootpath Compressed path * @throws FileNotFoundException * @throws Thrown when a file is not found IOException when the compression process is faulted */private static void ZipFile (file re SFile, Zipoutputstream zipout, String rootpath) throws FileNotFoundException, IOException {RootPath = R Ootpath + (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 ZipEntry (RootPath));            int reallength;            while ((Reallength = in.read (buffer))! =-1) {zipout.write (buffer, 0, reallength);            } in.close ();            Zipout.flush ();        Zipout.closeentry (); }    }}

Zip Decompression in Android

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.