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)
- Get and traverse each node
- Process nodes individually
- If the node is a directory
- Get the folder name and stitch it back to the target path
- Path name transcoding
- Call mkdir () to create the folder
- If the node is a file
- Get the actual target file name based on the node name and the specified pathname
- Use
/ the split node name string (except the last one is the path)
- If the string array length is greater than 1 after the split, the folder must be created first
- 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
- 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
- Create an output stream from a target file, create an input stream from a node
- Read data from the input stream to the output stream, see [Flow operation] [flow Operation]:http://blog.csdn.net/xmh19936688/article/details/50243997
- 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