2 Replies Latest reply on Mar 31, 2010 3:27 PM by tw12lveam

    open zip files

    srinth

      Hi ,

       

      can we open zip files.

        • 1. Re: open zip files
          rkewlani Level 2

          Here's a small piece of code which shall do the job.

           

           

          public function unzipFile(sourceFile:String,targetPath:String):Boolean{ //Takes source zip file & output directory as arguments
                                               var flNameLength:uint;
                                               var xfldLength:uint;
                                            var offset:uint;
                                            var compSize:uint;
                                            var uncompSize:uint;
                                            var compMethod:uint;
                                            var signature:uint;     
                                            var zfile:File = File.applicationStorageDirectory.resolvePath(sourceFile);
                                            var zStream:FileStream = new FileStream();
                                             try{
                                                      zStream.open(zfile, FileMode.READ);
                                                      bytes.endian =  Endian.LITTLE_ENDIAN;
                                             }
                                             catch(e:Error)
                                             {
                                                  Alert.show("Extraction failed -> " +e.message + "\n" + zfile.nativePath);
                                             }
                                             try
                                             {
                                                 while (zStream.position < zfile.size){
                                                                zStream.readBytes(bytes, 0, 30);
                                                               bytes.position = 0;
                                                                signature = bytes.readInt();
                                                                
                                                                if (signature != 0x04034b50)
                                                                         break;
                                                               bytes.position = 8;
                                                                compMethod = bytes.readByte(); 
                                                                 offset = 0;   
                                                                bytes.position = 26; 
                                                                flNameLength = bytes.readShort();   
                                                                offset += flNameLength;    
                                                                bytes.position = 28;   
                                                                xfldLength = bytes.readShort();
                                                                offset += xfldLength;   
                                                                
                                                                zStream.readBytes(bytes, 30, offset);
                                                                bytes.position = 30;
                                                                fileName = bytes.readUTFBytes(flNameLength);
                                                                if (fileName.substr(fileName.length - 1, 1) != '/')
                                                                    {
                                                                         bytes.position = 18;
                                                                         compSize = bytes.readUnsignedInt();
                                                                         bytes.position = 22;
                                                                         uncompSize = bytes.readUnsignedInt();

                                                                         zStream.readBytes(bytes, 0, compSize);
                                        
                                                                         if (compMethod == 8 )
                                                                                  bytes.uncompress(CompressionAlgorithm.DEFLATE);
                                        
                                                                         writeFile(targetPath, fileName, bytes);
                                                                }
                                        
                                                }
                                                  }
                                                  catch(e:Error)
                                                  {
                                                       Alert.show("Extraction failed -> " +e.message);
                                                       installDir.deleteDirectory(true);
                                                       return false;
                                                  }
                                                  return true;
                                                }


          public function writeFile(baseDir:String,fileName:String,data:ByteArray):void{
                                                var outFile:File=File.applicationStorageDirectory;
                                                outFile=outFile.resolvePath(baseDir+File.separator+fileName);
                                                var outStream:FileStream=new FileStream();
                                                outStream.open(outFile,FileMode.WRITE);
                                                outStream.writeBytes(data, 0, data.length);
                                                outStream.close();
                                                }


          • 2. Re: open zip files
            tw12lveam

            I use http://codeazur.com.br/lab/fzip/

             

            Works great/fast and you can create zips too.

             

            If you need something that can write addler32 checksums, look at http://nochump.com/blog/?p=15  ; however, I have had problems with this library, especially when it comes to writing two zip at the same time.