Once all the operations you wish to perform are added to the queue, a call to processQueue() will perform the operations in the order they were added to the queue. Operations performed on the queue throw any errors that occur out to the observer.

Attempting to perform a synchronous operation on the interface while the background queue is in progress will throw an NS_ERROR_IN_PROGRESS exception.

File and directory names should use slashes ("/") as separators and should not begin with a slash.

Note: Although it is not necessary to add directory entries in order to add file entries within them, some Zip utilities may have problems with that, so it may be best to add the directory entries explicitly first.

If true, the operation is queued for later execution. If false, the operation is performed immediately. Will be performed when processQueue() is called.

Exceptions thrown

NS_ERROR_NOT_INITIALIZED

No Zip file is open.

NS_ERROR_FILE_ALREADY_EXISTS

The specified path already exists in the Zip file.

NS_ERROR_IN_PROGRESS

The Zip writer is already performing another operation.

addEntryDirectory()

Adds a new directory entry to the Zip file. If aZipEntry does not end with "/" then it will be added.

Note: Although it is not necessary to add directory entries in order to add file entries within them, some Zip utilities may have problems with that, so it may be best to add the directory entries explicitly first.

In the above example, the file ("add this file.txt") located at "C:\add this file.txt" will be added to the zip file "C:\MyZipFile.zip", however in the zip file this file ("add this file.txt") will be named "blah.txt". If saveInZipAs was equal to "add this file.txt" then in the zip file it will also be named "add this file.txt"

Further Detail: This example gives futher detail on the importance of forward slashes

var fileToAddToZip = FileUtils.File('C:\\add this file.txt');
var saveInZipAs = 'sub folder/blah.txt'; //DO NOT USE BACKWARD SLASH for example do not do: 'sub folder\\blah.txt'. Using backward slash will not throw any errors but if this was an xpi file, firefox would not be able to read it properly.
zw.addEntryFile(saveInZipAs, Ci.nsIZipWriter.COMPRESSION_NONE, fileToAddToZip, false);

This example creates a folder in the zip called "sub folder" and then adds "C:\add this file.txt" to it, but it will be called "blah.txt".

aModTime

The modification time of the entry in microseconds.

aQueue

If true, the operation is queued for later execution. If false, the operation is performed immediately. Will be performed when processQueue() is called.

Exceptions thrown

NS_ERROR_NOT_INITIALIZED

No Zip file is open.

NS_ERROR_FILE_ALREADY_EXISTS

The specified path already exists in the Zip file.

NS_ERROR_IN_PROGRESS

The Zip writer is already performing another operation.

addEntryFile()

Adds a new file or directory to the Zip file. If the specified file is a directory, this call is equivalent to:

If true, the operation is queued for later execution. If false, the operation is performed immediately. Will be performed when processQueue() is called.

Exceptions thrown

NS_ERROR_NOT_INITIALIZED

No Zip file is open.

NS_ERROR_FILE_ALREADY_EXISTS

The specified path already exists in the Zip file.

NS_ERROR_IN_PROGRESS

The Zip writer is already performing another operation.

close()

Closes the Zip file.

void close();

Parameters

None.

Exceptions thrown

NS_ERROR_NOT_INITIALIZED

No Zip file is open.

NS_ERROR_IN_PROGRESS

The Zip writer is already performing another operation.

Other errors may be thrown if a failure to complete the Zip file occurs.

getEntry()

Returns a specified entry or null if there is no such entry in the current Zip file.

nsIZipEntry getEntry(
in AUTF8String aZipEntry
);

Parameters

aZipEntry

The path of the file entry to get.

Return value

An nsIZipEntry object describing the specified entry, or null if there is no such entry.

hasEntry()

Determines whether or not a specific entry exists in the Zip file.

boolean hasEntry(
in AUTF8String aZipEntry
);

Parameters

aZipEntry

The path of the file entry to check for.

Return value

true if there is an entry with the given path in the Zip file, otherwise returns false.

open()

Opens the specified Zip file.

void open(
in nsIFile aFile,
in PRInt32 aIoFlags
);

Parameters

aFile

The Zip file to open.

aIoFlags

The open flags for the Zip file, from prio.h.

Exceptions thrown

NS_ERROR_ALREADY_INITIALIZED

A Zip file is already open.

NS_ERROR_INVALID_ARG

The aFile parameter is null.

NS_ERROR_FILE_NOT_FOUND

The specified file was not found and the flags didn't permit creating it. Or the directory for the file does not exist.

NS_ERROR_FILE_CORRUPTED

The specified file is not a recognizable Zip file.

Other errors may be thrown upon failing to open the file, such as if the file is corrupt or in an unsupported format.

processQueue()

Processes all queued items until the entire queue has been processed or an error occurs. The observer is notified when the first operation begins and when the last operation completes.

Any failures are passed to the observer.

The Zip writer will be busy until the queue is complete or an error halts processing of the queue early. In the event of an early failure, the remaining items stay in the queue; calling processQueue() again will continue where operations left off.

The argument myZipFilePath isn't a path but rather it has to be an nsIFile instance specifying the location of the new zip file. The file need not exist, but the directory that contains it (nsIFile.parent) must exist.

Recursively Add All Contents of a Directory to Zip File

This example below can be copied and pasted into your scratchpad, set the environment to "Browser" and can run. What it does is shows a File Picker dialog and asks you to pick a folder. Once you pick a folder, it will create a zip file inside this folder with the same name as the folder. It will then recursively go through all contents in the folder selected and add them into this zip file. This example is also a good of example of the in AUTF8String aZipEntry argument of addEntryFile and how it is used, this entry is very important, it is basically what to save the file as in the zip, you can take a file "blah.exe" and make it save in the zip as "hi and no extension".