Zipping a file and saving it to local disk using Actionscript 3 & FZip - actionscript-3

i made a flash desktop application that requires the user to save a file to his computer, in this case i have a lot of images and i want the user to save them on his computer, the images are on a folder called "imgs", i'm using one image in this case, i tried to do the following using FZip:
import deng.fzip.*;
import flash.filesystem.*;
import flash.filesystem.File;
//retrieve the image from the "imgs" folder
var myfile:File = new File("C:/Users/User/Desktop/zip/imgs/myimage.png");
//zipping the image
var zip:FZip = new FZip();
zip.addFile(myfile.name, myfile.data);
//saving the image to the user's desktop
var ba:ByteArray = new ByteArray();
zip.serialize(ba);
ba.position = 0;
var finalZIP:File = File.desktopDirectory.resolvePath("myfile.zip");
var fs:FileStream = new FileStream();
fs.open(finalZIP, FileMode.WRITE);
fs.writeBytes(ba);
fs.close();
the result is a zip file in the desktop, named "myfile.zip", with and image named "myimage.png", but when i unzip the file the image is empty, i tried to do the same with a text file, the text file is there zipped, but with nothing inside it.
Any idea what might cause this problem?

Related

How to share the image in the application as3

I am using the Android-Sharing-Extension-ANE.
How to share an image as result from my application code?
This code does not work
var bitmap:Bitmap = ...;
// encoding image by native encoder (availible on FP 11.3/AIR 3.3 or newer)
var bitmapBytes:ByteArray = bitmap.bitmapData.encode(new Rectangle(0, 0, bitmap.width, bitmap.height), new JPEGEncoderOptions(70)));
var file:File = File.documentsDirectory.resolvePath("image_for_share.jpg");
var stream:FileStream = new FileStream(); // write file to local memory
stream.open(file, FileMode.WRITE);
stream.writeBytes(fileBytes);
stream.close();
SharingExtension.shareImage(file, "Choser title", "Message"));
"How do I share an image with this ANE I use Animate CC?"
From the "Read Me" of that ANE:
How to use:
Connect com.illuzor.extensions.SharingExtension.ane file to your
Android AIR project.
Import com.illuzor.sharingextension.SharingExtension;
Since it's not clear what your exact problem is (because not enough information given)...
(1) Did you import the required Class files?
import com.illuzor.sharingextension.SharingExtension;
btn_Share.addEventListener (MouseEvent.CLICK, shareAndroid);
function shareAndroid (event: MouseEvent): void
{
//# If this works then replace with bitmap code (use shareImage)
SharingExtension.shareText ("AS3 Test", "This text is from AS3 code");
}
(2) Did you add (connect) the ANE to your project (in Settings)?
Read this for advise on adding an ANE: https://www.adobe.com/devnet/air/articles/using-ane-in-flash.html

FLEX/AS3 How to open docx/odt file

I have function that create docx or odt file. I need automaticly to open this file in microsoft/libre office, right after creation complete. How to coding this in flex/as3 ?
protected function create003(docType:String, patientID:String):void
{
create003Result.token = nhealthReports.create003(docType, patientID);
}
protected function getFormModuleDataResult_resultHandler(event:ResultEvent):void
{
var pathToFile:String;
pathToFile=create003Result.lastResult; // this is path to created file
// here i need some code from you
}
<nhealthreports:NhealthReports id="nhealthReports"
showBusyCursor="true"/>
<s:CallResponder id="create003Result" result="getFormModuleDataResult_resultHandler(event)"/>
So you'd need to first download the file to the user's machine and then open it. Something like this should do it (copy pasted stuff from my projects so you might need to adjust it a bit).
Also your server might need a crossdomain file so your app can load files from it.
private function getFormModuleDataResult_resultHandler(event:ResultEvent):void
{
// load file
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoadingComplete);
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.load(new URLRequest(pathToFile));
}
private function onLoadingComplete(event:Event):void
{
// get the data as bytearray
var data:ByteArray = event.target.data;
// you will probably need to figure this out from your server path or define your own here
var fileName:String = "MyFilename.doc";
// create a file under the application storage directory (C:\Users\YOURUSERHERE\AppData\Roaming\RateBook\Local Store)
// you can store the file anywhere but it is recommended to do it here
// as users with restricted access on their machines (non-admin users) might have trouble saving the files elsewhere
var file:File = File.applicationStorageDirectory.resolvePath(fileName);
//create a file stream to be able to write the content of the file
var fileStream:FileStream = new FileStream();
//open the file stream and set for Write
fileStream.open(file, FileMode.WRITE);
//writes the bytes
fileStream.writeBytes(data, 0, data.length);
//close the stream
fileStream.close();
// by now the file should be saved to disk, let's open it
// Naturally this assumes that the user have the file extension (like .doc) associated with the correct program (MS Word)
file.openWithDefaultApplication();
}

How do I add exif data to an image in AS3?

How do I add exif data to a JPG or PNG in AS3?
var x:JPEGEncoderOptions = new JPEGEncoderOptions();
var jpegEncoder:JPEGEncoder = new JPEGEncoder(100);
var bitmapData:BitmapData = new BitmapData(100,100,true,0x00000000);
bitmapData.drawWithQuality(this);
var ba:ByteArray = jpegEncoder.encode(bitmapData);
var file:FileReference = new FileReference();
file.save(ba, "ScreenShot.jpeg");
More info
In my app we are letting the user take a screen shot using the PNGEncoder and JPEGEncoder class. They've asked to be able to save the Author creation date and time in Exif. I'm able to create the bytearray and then save that file but I don't know how to add the exif data to it.

How to add two UILoader inside a UILoader and save the contents of the UILoader using actionscript 3

I have been into a research these past hours in figuring out how to add a UILoader inside a UILoader and save all the contents of a UILoader. I have two instance of UILoader (loader_inner and loader_outer) and they are both placed manually using the UILoader component.
The code goes something like this:
loader_inner.load(new URLRequest("background_1.jpg"));
loader_outer.load(new URLRequest("rabbit.jpg"));
loader_outer.addChild(loader_inner);
var bitmapData:BitmapData = new BitmapData(loader_outer.width,loader_outer.height);
bitmapData.draw(loader_outer);
var jpgencoder:JPGEncoder = new JPGEncoder(100);
var mybyte:ByteArray = jpgencoder.encode(bitmapData);
var myfile:FileReference = new FileReference();
myfile.save(mybyte,"test.jpg");
The problem is during save, I can only see the a white image in file but not the two contents. I am expecting a background with a rabbit in the image file that is placed exactly on the two components drawn manually in the work space.
UILoader.load is an asynchronous action. As such, you are attempting to draw the loader to a bitmap before it has actually had a chance to load the images. You will need to listen for COMPLETE events on both inner and outer loaders:
import com.adobe.images.JPGEncoder;
import flash.display.Bitmap;
import flash.events.Event;
loader_inner.addEventListener(Event.COMPLETE, onLoadComplete);
loader_outer.addEventListener(Event.COMPLETE, onLoadComplete);
loader_inner.load(new URLRequest("background_1.jpg"));
loader_outer.load(new URLRequest("rabbit.jpg"));
loader_outer.addChild(loader_inner);
function onLoadComplete(e:Event):void
{
// check to make sure that *both* loaders have fully loaded
if(loader_inner.percentLoaded && loader_outer.percentLoaded)
{
var bitmapData:BitmapData = new BitmapData(loader_outer.width,loader_outer.height);
bitmapData.draw(loader_outer);
var jpgencoder:JPGEncoder = new JPGEncoder(100);
var mybyte:ByteArray = jpgencoder.encode(bitmapData);
var myfile:FileReference = new FileReference();
myfile.save(mybyte,"test.jpg");
}
}

zip many files with Air as3 flash

OK . I am reasonably capable, but still learning.
This is for a program I am writing in AIR.
Basicaly I am needing to grab the files of mixed type and content from a given folder and zip them.
Here is the code I have put together and it sort of works.
Problem I have is that the files in the zip all have zero byte's in them. Efectivly empty.
What have I missed or done wrong?
import flash.filesystem.File;
import flash.events.Event;
import deng.fzip.*;
var directory:File = File.desktopDirectory.resolvePath("FOLDER/");
var zip:FZip = new FZip();
var files:Array = directory.getDirectoryListing();
for(var i:uint = 0; i < files.length; i++)
{
zip.addFile(files[i].name, files[i].data);
trace(files[i].name);
}
var ba:ByteArray = new ByteArray();
zip.serialize(ba);
ba.position = 0;
var finalZIP:File = File.desktopDirectory.resolvePath("TEST.zip");
var fs:FileStream = new FileStream();
fs.open(finalZIP, FileMode.WRITE);
fs.writeBytes(ba);
fs.close();
EDIT=:
While running the code, I have noticed this in the errors panel.
....app\scripts_classes\deng\fzip\FZipFile.as, Line 362 Warning: 1106: Empty statement found where block of code expected. Did you type ';' accidentally?
and it seems to be fine from what I can see, but then I did not write the Fzip script.
File.data is only populated after a call to File.load.
For synchronous loading of bytes, look at FileStream. These docs give a rundown.