as3 解压缩
下例包括了解zip 和压zip
用到类库:
package
{
import com.adobe.images.JPGEncoder;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.utils.ByteArray;
import nochump.util.zip.ZipEntry;
import nochump.util.zip.ZipFile;
import nochump.util.zip.ZipOutput;
public class Main extends Sprite
{
private var myFileReference:FileReference;
public function Main() {
init();
}
private function init():void {
Debug.clear();
open_btn.label = "解压";
save_btn.label = "压缩";
open_btn.addEventListener(MouseEvent.CLICK, openHandler);
save_btn.addEventListener(MouseEvent.CLICK, saveHandler);
}
private function saveHandler(e:MouseEvent):void {
saveZip();
}
private function openHandler(e:MouseEvent):void {
readZip();
}
//压zip
private function saveZip():void {
var fileName:String = "helloworld.txt";
var fileName2:String = "image.jpg";
var fileData:ByteArray = new ByteArray();
fileData.writeUTFBytes("Hello World!中文");
var zipOut:ZipOutput = new ZipOutput();
// Add entry to zip
var ze:ZipEntry = new ZipEntry(fileName);//这是一个文件
var ze2:ZipEntry = new ZipEntry(fileName2);//这是一个文件
//
//var jpg = new bmd(100, 100);
var jpg:BitmapData = new BitmapData(stage.width, stage.height);
jpg.draw(stage);
var je:JPGEncoder = new JPGEncoder();
var pixels:ByteArray = je.encode(jpg);
zipOut.putNextEntry(ze2);
zipOut.write(pixels);
//zipOut.closeEntry();
//
zipOut.putNextEntry(ze);
zipOut.write(fileData);
zipOut.closeEntry();
// end the zip
zipOut.finish();
// access the zip data
var zipData:ByteArray = zipOut.byteArray;
myFileReference = new FileReference();
myFileReference.save(zipData, "qq.zip");
}
private function onCom(e:Event):void {
Debug.warning("com");
}
//解zip
private function readZip():void{
var urlStream:URLStream = new URLStream();
urlStream.addEventListener(Event.COMPLETE, completeHandler);
urlStream.load(new URLRequest( "bin.zip" ));
}
private function completeHandler(event:Event):void {
var datastream:URLStream = URLStream(event.target);
var kmzFile:ZipFile = new ZipFile(datastream);
//totalMaterials = kmzFile.entries.join("@").split(".jpg").length;
Debug.log(kmzFile);
Debug.log(kmzFile.entries);
Debug.log(kmzFile.entries.length);//总文件数
Debug.log(kmzFile.entries.join("@").split(".jpg").length);//顶目录的长度
for(var i:int = 0; i < kmzFile.entries.length; i++) {
var entry:ZipEntry = kmzFile.entries[i];
var data:ByteArray = kmzFile.getInput(entry);
if((entry.name.indexOf(".jpg")>-1 || entry.name.indexOf(".png")>-1)) {
var _loader : Loader = new Loader();
_loader.name = entry.name.split("/").reverse()[0].split(".")[0];
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadBitmapCompleteHandler);
_loader.loadBytes(data);
}else if (entry.name.indexOf(".txt") > -1) {
Debug.log(data.toString());
}
};
};
private function loadBitmapCompleteHandler(e:Event):void {
var loader:Loader = Loader(e.target.loader);
var bitmap:Bitmap = Bitmap(loader.content);
addChild(bitmap);
Debug.warning(this.numChildren);
}
}
}