Zip
Zip
Add any files and create a zip file with simple user interface.
/**
* Copyright kimo0517 ( http://wonderfl.net/user/kimo0517 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wbTX
*/
package
{
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.text.TextField;
import flash.utils.ByteArray;
import flash.geom.Rectangle;
import nochump.util.zip.ZipEntry;
import nochump.util.zip.ZipOutput;
import com.bit101.components.PushButton;
import com.bit101.components.VScrollBar;
[SWF(width=468, height=468, backgroundColor=0xFFFFFF)]
public class FlashZip extends Sprite
{
private var loadingSprite:Sprite;
private var fr:FileReference;
private var loader:Loader;
private var loadedByteArrays:Vector.<ByteArray>;
private var namesOnScreen:Vector.<TextField>;
private var removeButtons:Vector.<PushButton>;
private var btnAdd:PushButton;
private var btnGenZip:PushButton;
private var warningTF:TextField;
private var zipOutput:ZipOutput;
private var scrollPlane:Sprite;
private var scrollBar:VScrollBar;
public function FlashZip():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
loadingSprite = new Sprite;
loadingSprite.graphics.beginFill(0x000000, 0.6);
loadingSprite.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
loadingSprite.graphics.endFill();
var nowLoadingTF:TextField = new TextField;
nowLoadingTF.autoSize = "left";
nowLoadingTF.multiline = false;
nowLoadingTF.text = "Processing...";
nowLoadingTF.x = stage.stageWidth / 2 - nowLoadingTF.width / 2;
nowLoadingTF.y = stage.stageHeight / 2 - nowLoadingTF.height / 2;
loadingSprite.addChild(nowLoadingTF);
loadingSprite.mouseChildren = false;
btnAdd = new PushButton(this, 5, 5, "Add File", _btnAddClicked);
btnGenZip = new PushButton(this, btnAdd.x + btnAdd.width + 5, 5, "Create Zip", _btnGenZipClicked);
warningTF = new TextField;
warningTF.autoSize = "left";
warningTF.textColor = 0xFF0000;
warningTF.y = 5;
warningTF.x = btnGenZip.x + btnGenZip.width + 5;
addChild(warningTF);
loadedByteArrays = new Vector.<ByteArray>();
//loadedNames = new Vector.<String>();
namesOnScreen = new Vector.<TextField>();
removeButtons = new Vector.<PushButton>();
zipOutput = null;
graphics.lineStyle(1, 0x999999);
graphics.moveTo(0, btnAdd.y + btnAdd.height + 5);
graphics.lineTo(stage.stageWidth, btnAdd.y + btnAdd.height + 5);
scrollPlane = new Sprite;
scrollPlane.y = btnAdd.y + btnAdd.height + 10;
scrollPlane.scrollRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight - scrollPlane.y);
addChild(scrollPlane);
scrollBar = new VScrollBar(this, 0, scrollPlane.y, _scrollBarChanged);
scrollBar.x = stage.stageWidth - scrollBar.width;
scrollBar.height = scrollPlane.scrollRect.height;
scrollBar.visible = false;
}
private function _btnAddClicked(e:MouseEvent):void {
fr = new FileReference();
fr.addEventListener(Event.SELECT, _frStartLoad);
fr.addEventListener(Event.COMPLETE, _frLoadComplete);
var filter:FileFilter = new FileFilter("Any files", "*");
fr.browse([filter]);
}
private function _frStartLoad(e:Event):void {
for (var i:int = 0; i < removeButtons.length; i++) {
if (namesOnScreen[i].text == fr.name) {
warningTF.text = "Cannot open duplicate files";
return; //cannot add two files with same name
}
}
warningTF.text = "";
mouseChildren = false;
addChild(loadingSprite);
fr.load();
}
private function _frLoadComplete(e:Event):void {
mouseChildren = true;
if (loadingSprite.parent) {
removeChild(loadingSprite);
}
//loadedNames.push(fr.name);
loadedByteArrays.push(fr.data);
var newRemoveButton:PushButton = new PushButton(scrollPlane, 5, 0, "remove", _removeClicked);
removeButtons.push(newRemoveButton);
var newTF:TextField = new TextField;
newTF.autoSize = "left";
newTF.text = fr.name;
newTF.x = newRemoveButton.x + newRemoveButton.width + 5;
scrollPlane.addChild(newTF);
namesOnScreen.push(newTF);
_updateScreen();
}
private function _removeClicked(e:MouseEvent):void {
var index:int = removeButtons.indexOf(e.currentTarget, 0);
scrollPlane.removeChild(removeButtons[index]);
scrollPlane.removeChild(namesOnScreen[index]);
loadedByteArrays[index].clear();
removeButtons.splice(index, 1);
namesOnScreen.splice(index, 1);
loadedByteArrays.splice(index, 1);
_updateScreen();
}
private function _updateScreen():void {
var currentY:Number = 0;
for (var i:int = 0; i < removeButtons.length; i++) {
removeButtons[i].y = currentY;
namesOnScreen[i].y = currentY;
currentY += removeButtons[i].height + 5;
}
scrollPlane.scrollRect = new Rectangle(0,0,scrollPlane.scrollRect.width, scrollPlane.scrollRect.height);
if (currentY < scrollPlane.scrollRect.height) {
scrollBar.visible = false;
stage.removeEventListener(MouseEvent.MOUSE_WHEEL, _mouseWheel);
} else {
scrollBar.visible = true;
scrollBar.setThumbPercent(scrollPlane.scrollRect.height / currentY);
scrollBar.setSliderParams(0, (currentY - scrollPlane.scrollRect.height)/10, 0);
stage.addEventListener(MouseEvent.MOUSE_WHEEL, _mouseWheel);
}
}
private function _btnGenZipClicked(e:MouseEvent):void {
if (namesOnScreen.length == 0) {
return;
}
var zipEntry:ZipEntry;
mouseChildren = false;
addChild(loadingSprite);
if (zipOutput != null) {
zipOutput.byteArray.clear();
}
zipOutput = new ZipOutput;
for (var i:int = 0; i < namesOnScreen.length; i++) {
zipEntry = new ZipEntry(namesOnScreen[i].text);
zipOutput.putNextEntry(zipEntry);
zipOutput.write(loadedByteArrays[i]);
zipOutput.closeEntry();
}
zipOutput.finish();
fr = new FileReference;
fr.addEventListener(Event.COMPLETE, _frSaveComplete);
fr.addEventListener(Event.CANCEL, _frSaveComplete);
fr.save(zipOutput.byteArray, "kimo0517.zip");
}
private function _frSaveComplete(e:Event):void {
zipOutput.byteArray.clear();
zipOutput = null;
mouseChildren = true;
if (loadingSprite.parent) {
removeChild(loadingSprite);
}
}
private function _scrollBarChanged(e:Event):void {
scrollPlane.scrollRect = new Rectangle(0,scrollBar.value * 10-5,scrollPlane.scrollRect.width, scrollPlane.scrollRect.height);
}
private function _mouseWheel(e:MouseEvent):void {
scrollBar.value -= e.delta;
_scrollBarChanged(null);
}
}
}