Search code examples
actionscript-3airdocx

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 corrupt 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


Solution

  • 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)