Create a docx using FZIP in Actionscript 3 - Adobe AIR
Hello I will try my best to explain my problem and I really hope someone can figure out what could be wrong.
Situation:
Using the office open XML format by microsoft I was able to create the files needed for a clean Word document. These files are all XML files. What I do is write the XML content to a different bytearray for each file.
I then add the bytearray's to the FZIP library (http://codeazur.com.br/lab/fzip/) and create a docx file using the following code:
var zip:FZip = new FZip();
zip.addFile("/_rels/", null, false);
zip.addFile("/docProps/", null, false);
zip.addFile("/word/", null, false);
zip.addFile("[Content_Types].xml", bytContentType, false);
zip.addFile("/docProps/app.xml", bytApp, false);
zip.addFile("/docProps/core.xml", bytCore, false);
zip.addFile("/word/document.xml", bytDocument, false);
zip.addFile("/word/fontTable.xml", bytFontTable, false);
zip.addFile("/word/settings.xml", bytSettings, false);
zip.addFile("/word/webSettings.xml", bytWebSettings, false);
zip.addFile("/_rels/.rels", bytRels, false);
var file:File = File.documentsDirectory;
file = file.resolvePath(folder + ".docx");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
zip.serialize(stream);
stream.close();
Ok this creates a new docx file in my documents. If I rename the .docx to .zip and unzip it it will follow the exact structure needed for Word documents (example: http://dl.dropbox.com/u/5490354/Screen%20shot%202011-05-08%20at%2019.40.10.png) In this example "Untitled Document" was originally "Untitled document.docx" (then renamed to .zip and extracted)
So you can see everything works fine untill... you try to open the "Untitled document.docx" in Word. It will say the file is corrupt.
WHAT DOES WORK HOWEVER... After extracting the docx file (like in screenshot) when you select all the files/folders inside the Untitled Document folder and ZIP those, then rename ZIP file to docx (see result: http://dl.dropbox.com/u/5490354/Screen%20shot%202011-05-08%20at%2019.44.18.png) it works fine in Word and word reads the file correctly. NOTE: If you try to zip and rename the Untitled Document folder again it will become co开发者_如何学运维rrupt again as well.
So my question I hope the description was clear and now I am hoping for someone who knows what could be the problem that the word file is corrupt when zipped using FZIP in actionscript 3 but works fine when you ZIP the files after extracting your corrupt file.
Thanks
This is just a chance suggestion, since I can't follow the code: but is it somehow making a zipped directory for you, rather than just zipping together an accumulation of files? It should be the latter. (It does look like what your source is doing, admittedly.)
This makes a big difference as I found out once: see How to zip a WordprocessingML folder into readable docx -- in particular the excellent answer of Eric White.
I see that in your image of the result of unzipping Untitled Document.zip
, there is an outer directory also called Untitled Document
. If you did not make this directory separately, but it emerged from unzipping, then I think that may be the trouble. (Notice that if you just rename a random .docx
file .zip
and open it, the bits like [Content_Types].xml
will be spread out all over the directory that has the zipfile in it, rather than tastefully making a new directory for themselves; so the .docx
file isn't itself a zipped directory.) Another way of putting the point is, if you rename Archive.docx
to Archive.zip
, and unzip, does it overwrite the files in the directory, or make a new directory? If Word opens it, assume it's the former.
I found the solution to the problem. It was an honest mistake but an annoying one
zip.addFile("/docProps/app.xml", bytApp, false);
Should be
zip.addFile("docProps/app.xml", bytApp, false);
Same on all other addFile elements (the first slash needs to be removed)
精彩评论