Adobe AIR:压缩Zip/创建zip文件

Following up on my previous post, here's how to archive a bunch of files and make a zip file.

After getting all the files you want to put in the archive, create a new ZipEntry object for each file and add it to a ZipOutput object using the putNextEntry() of the ZipOutput class. Then pass the ByteArray data of the file to the write() method of the ZipOutput class and close the entry by using the closeEntry() method. Finally, call the finish() method once you have done the same for all the files.

Here's the code snippet :

   import flash.events.FileListEvent;
   import flash.filesystem.*;
   
   import nochump.util.zip.*;
   
   private var zipInput:File = new File();
   private var zipOutput:File = new File();
   private var zipFile:ZipOutput;
   
   private var files:Array = new Array();
   
   private function loadFiles():void
   {
    zipInput.browseForOpenMultiple("Open ZIP file");
    zipInput.addEventListener(FileListEvent.SELECT_MULTIPLE, onSelect);
   }
   
   private function onSelect(e:FileListEvent):void
   {
    for(var i:uint = 0;i < e.files.length;i++)
    {
     var stream:FileStream = new FileStream();
     var f:File = e.files[i] as File;
     stream.open(f,FileMode.READ);
     var fileData:ByteArray = new ByteArray();
     stream.readBytes(fileData);
     var file:Object = new Object();
     file.name = f.name;
     file.data = fileData;
     files.push(file);
    }
   }
   
   private function createZIP():void
   {
    zipFile = new ZipOutput();
    for(var i:uint = 0; i < files.length; i++)
    {
     var zipEntry:ZipEntry = new ZipEntry(files[i].name);
     zipFile.putNextEntry(zipEntry);
     zipFile.write(files[i].data);
     zipFile.closeEntry();
    }
    zipFile.finish();
    
    zipOutput.browseForSave("Select target directory");
    zipOutput.addEventListener(Event.SELECT, onSave);
   }
   
   private function onSave(e:Event):void
   {
    var archiveFile:File = e.target as File;
    if(!archiveFile.exists)
    {
     var stream:FileStream = new FileStream();
     stream.open(archiveFile,FileMode.WRITE);
     stream.writeBytes(zipFile.byteArray);
     stream.close();
    }
   }


As usual you can download the project archive here. Happy experimenting.

 

from: http://pradeek.blogspot.com/2009/05/creating-zip-files-with-adobe-air-with.html#comments

 

 

代码中提到的nochump lib见附件。

 

其主页如下:

 

http://nochump.com/blog/?index.html

 

友情提示:博客地址如果打不开,请FQ。◕‿◕

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值