package  {
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	import flash.utils.CompressionAlgorithm;
	import flash.events.Event;
	import flash.display.Sprite;

	import fl.controls.TextArea;	
	import fl.controls.Label;
	
	public class uzipfile extends Sprite {
		

		public function uzipfile() {
			init();		 
			
		}

      public function init():void  { 
	        var taFiles:TextArea = new TextArea();
		   
	  
			taFiles.setSize(380, 150);
			taFiles.move(10, 130);

			
			addChild(taFiles);
			
			  var output:Label= new Label();
			  
			  output.move(10, 100);
			output.width = 150;
			  output.text = "app1.zip文件内容如下: ";
			addChild(output);
			
			
			var bytes:ByteArray = new ByteArray();
			
			// variables for reading fixed portion of file header
			var fileName:String = new String();
			var flNameLength:uint;
			var xfldLength:uint;
			var offset:uint;
			var compSize:uint;
			var uncompSize:uint;
			var compMethod:int;
			var signature:int;
	     // File variables for accessing .zip file
		var zfile:File = File.desktopDirectory;
		    zfile = zfile.resolvePath("app1.zip");
		
		var zStream:FileStream = new FileStream();
	  
	  	zStream.open(zfile, FileMode.READ);
		bytes.endian = Endian.LITTLE_ENDIAN;
		while (zStream.position < zfile.size) {
			// read fixed metadata portion of local file header
			zStream.readBytes(bytes, 0, 30);
			bytes.position = 0;
			signature = bytes.readInt();
			// if no longer reading data files, quit
			if (signature != 0x04034b50) {
				break;
			}
			bytes.position = 8;
			compMethod = bytes.readByte(); // store compression method (8 == Deflate)
			offset = 0;// stores length of variable portion of metadata
			bytes.position = 26; // offset to file name length
			flNameLength = bytes.readShort();// store file name
			offset += flNameLength; // add length of file name
			bytes.position = 28;// offset to extra field length
			xfldLength = bytes.readShort();
			offset += xfldLength;// add length of extra field
			// read variable length bytes between fixed-length header and compressed file data
			zStream.readBytes(bytes, 30, offset);
			
			bytes.position = 30;
			fileName = bytes.readUTFBytes(flNameLength); // read file name
			taFiles.appendText(fileName + " : \n"); // write file name to text area
			bytes.position = 18;
			compSize = bytes.readUnsignedInt(); // store size of compressed portion
			taFiles.appendText("\t压缩后的大小是: " + compSize + '字节；');
			bytes.position = 22; // offset to uncompressed size
			uncompSize = bytes.readUnsignedInt(); // store uncompressed size
			taFiles.appendText("\t解压缩后的大小是: " + uncompSize + '字节。\n');
			
			// read compressed file to offset 0 of bytes; for uncompressed files
			// the compressed and uncompressed size is the same
			if (compSize == 0) continue;
			zStream.readBytes(bytes, 0, compSize);
            if (compMethod == 8) // if file is compressed, uncompress
			{
				bytes.uncompress(CompressionAlgorithm.DEFLATE);
			}
			outFile(fileName, bytes); // call outFile() to write out the file
		}
		  
	  }
	   
	  
	  public function outFile(fileName:String, data:ByteArray):void
		{    
			var outFile:File = File.desktopDirectory; // dest folder is
			outFile = outFile.resolvePath(fileName); // name of file to write
			var outStream:FileStream = new FileStream();
			// open output file stream in WRITE mode
			outStream.open(outFile, FileMode.WRITE);
			// write out the file
			outStream.writeBytes(data, 0, data.length);
			// close it
			outStream.close();
		}
	  
	  
	}
	
}
