Android Buffet parsing zip

Source: Internet
Author: User

Android Buffet parsing zip

    • Android Buffet parsing zip
        • Get all node cursors for a file
        • Traverse each node
        • Working with a single node
        • Unzip the file idea to extract a zipfile to the specified destination path
        • Code to use directly

Get all node cursors for a file
Enumeration entries = zipFile.entries();//ZipFile为java.util.zip.ZipFile
Traverse each node
while(entries.hasMoreElements()){    //do something}
Working with a single node
ZipEntry ze = (ZipEntry)entries.nextElement();boolean b = ze.isDirectory();//是否是目录String name = ze.getName();获取文件/文件夹名称StringnewString(name.getBytes("8859_1"),"gb2312");//文件名转码new BufferedInputStream(zipFile.getInputStream(ze));//从该节点获取输入流(输入到内存,ze必须不是目录)zipFile.close();//关闭zip文件
Unzip the file idea (unzip a zipfile to the specified target path)
    1. Get and traverse each node
    2. Process nodes individually
      • If the node is a directory
        1. Get the folder name and stitch it back to the target path
        2. Path name transcoding
        3. Call mkdir () to create the folder
      • If the node is a file
        1. Get the actual target file name based on the node name and the specified pathname
          1. Use / the split node name string (except the last one is the path)
          2. If the string array length is greater than 1 after the split, the folder must be created first
        2. After traversing the segmented array of strings (except the last one) and stitching them into the target decompression path one after the other, transcode the new string and call Mkdirs () to create the folder sequentially
        3. The last string is transcoded and stitched onto the path string and the file is created by calling CreateNewFile (), at which point the target file for that node is obtained
        4. Create an output stream from a target file, create an input stream from a node
        5. Read data from the input stream to the output stream, see [Flow operation] [flow Operation]:http://blog.csdn.net/xmh19936688/article/details/50243997
    3. Close zip file
Code to use directly
/** Unzip the zip file to the specified path * / Public Static void Unpackzipfile(ZipFile zipfile,string FolderPath)    {Enumeration entries = Zipfile.entries (); ZipEntry ze=NULL;byte[] buf=New byte[1024x768];//Traverse node     while(Entries.hasmoreelements ()) {ze= (ZipEntry) entries.nextelement ();//region The node is a directory        if(Ze.isdirectory ()) {String dirstr= folderpath+ze.getname ();//Get folder name            Try{//Create a folderDirstr=NewString (Dirstr.getbytes ("8859_1"),"gb2312"); File f=NewFile (DIRSTR);            F.mkdir (); }Catch(Exception e)            {E.printstacktrace (); }Continue; }//endregion        //region The node is a file        Try{//Create IO stream copy fileOutputStream OS =NewBufferedoutputstream (NewFileOutputStream (Getrealfilename (FolderPath, Ze.getname ())); InputStream is =NewBufferedinputstream (Zipfile.getinputstream (Ze));intreadlen=0; while(Readlen=is.read (BUF,0,1024x768))!=-1) {Os.write (buf,0, Readlen);            } is.close ();        Os.close (); }Catch(Exception e)        {E.printstacktrace (); }//endregion}//Close file    Try{Zipfile.close (); }Catch(Exception e)    {E.printstacktrace (); }}/** gets and creates an absolute path based on a relative path * /Private StaticFileGetrealfilename(String basedir,string Absfilename) {string[] Dirs=absfilename.split ("/");//Get stepwise relative pathFile ret=NewFile (BaseDir);//To record each level of pathString substr=NULL;if(dirs.length>1) {//Loop traversal does not include the last node because the node is not a path and is handled independently         for(inti =0; I < dirs.length-1; i++) {substr = dirs[i];Try{substr =NewString (Substr.getbytes ("8859_1"),"gb2312"); }Catch(Exception e)            {E.printstacktrace (); } ret =NewFile (ret, substr);//Record the level path}//create each level directory        if(!ret.exists ())        {Ret.mkdirs (); }    }//processing the last nodesubstr=dirs[dirs.length-1];Try{substr=NewString (Substr.getbytes ("8859_1"),"gb2312"); }Catch(Exception e)    {E.printstacktrace (); } ret=NewFile (RET,SUBSTR);if(!ret.exists ()) {Try{Ret.createnewfile (); }Catch(Exception e)        {E.printstacktrace (); }    }returnRET;}

Android Buffet parsing zip

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.