--- title: nsIZipWriter slug: NsIZipWriter translation_of: Mozilla/Tech/XPCOM/Reference/Interface/nsIZipWriter ---
The nsIZipWriter
interface provides an easy way for scripts to archive data in the Zip file format. Operations on the archive can be performed one by one, or queued for later execution.
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.
Inherits from: nsISupports
Implemented by: @mozilla.org/zipwriter;1
. To create an instance, use:
var zipWriter = Components.classes["@mozilla.org/zipwriter;1"] .createInstance(Components.interfaces.nsIZipWriter);
void addDirectoryEntry(in AUTF8String aZipEntry, in PRTime aModTime, in boolean aQueue); |
void addEntryChannel(in AUTF8SZtring aZipEntry, in PRTime aModTime, in PRInt32 aCompression, in nsIChannel aChannel, in boolean aQueue) |
void addEntryFile(in AUTF8SZtring aZipEntry, in PRInt32 aCompression, in nsIFile aFile, in boolean aQueue) |
void addEntryStream(in AUTF8SZtring aZipEntry, in PRTime aModTime, in PRInt32 aCompression, in nsIInputStream aStream, in boolean aQueue) |
void close() |
nsIZipEntry getEntry(in AUTF8String aZipEntry); |
boolean hasEntry(in AUTF8String aZipEntry); |
void open(in nsIFile aFile, in PRInt32 aIoFlags); |
void processQueue(in nsIRequestObserver aObserver, in nsISupports aContext) |
void removeEntry(in AUTF8String aZipEntry, in boolean aQueue) |
Attribute | Type | Description |
comment |
ACString |
Gets or sets the comment associated with the currently open Zip file. Throws NS_ERROR_NOT_INITIALIZED if there isn't an open Zip file. |
inQueue |
boolean |
true if operations are being performed in the background queue, or false if background operations are not in progress. Read-only. |
file |
nsIFile |
The Zip file being written to. Read-only. |
Constant | Value | Description |
COMPRESSION_NONE |
0 | Don't compress the file. |
COMPRESSION_FASTEST |
1 | Use the fastest compression method when adding the file to the archive. |
COMPRESSION_DEFAULT |
6 | Use the default compression method when adding the file to the archive. |
COMPRESSION_BEST |
9 | Use the best compression method when adding the file to the archive. |
Adds a new directory entry to the Zip file.
void addEntryDirectory( in AUTF8String aZipEntry, in PRTime aModTime, in boolean aQueue );
aZipEntry
aModTime
aQueue
true
, the operation is queued for later execution. If false
, the operation is performed immediately.NS_ERROR_NOT_INITIALIZED
NS_ERROR_FILE_ALREADY_EXISTS
NS_ERROR_IN_PROGRESS
Adds data from a channel to the Zip file.
void addEntryChannel( in AUTF8String aZipEntry, in PRTime aModTime, in PRInt32 aCompression, in nsIChannel aChannel, in boolean aQueue );
aZipEntry
aModTime
aCompression
aChannel
aQueue
true
, the operation is queued for later execution. If false
, the operation is performed immediately.NS_ERROR_NOT_INITIALIZED
NS_ERROR_FILE_ALREADY_EXISTS
NS_ERROR_IN_PROGRESS
Adds a new file or directory to the Zip file. If the specified file is a directory, this call is equivalent to:
addDirectoryEntry(aZipEntry, aFile.lastModifiedTime, aQueue);
void addEntryFile( in AUTF8String aZipEntry, in PRInt32 aCompression, in nsIFile aFile, in boolean aQueue );
aZipEntry
aCompression
aFile
aQueue
true
, the operation is queued for later execution. If false
, the operation is performed immediately.NS_ERROR_NOT_INITIALIZED
NS_ERROR_FILE_ALREADY_EXISTS
NS_ERROR_IN_PROGRESS
Adds data from an input stream to the Zip file.
void addEntryStream( in AUTF8String aZipEntry, in PRTime aModTime, in PRInt32 aCompression, in nsIInputStream aStream, in boolean aQueue );
aZipEntry
aModTime
aCompression
aStream
aQueue
true
, the operation is queued for later execution. If false
, the operation is performed immediately.NS_ERROR_NOT_INITIALIZED
NS_ERROR_FILE_ALREADY_EXISTS
NS_ERROR_IN_PROGRESS
Closes the Zip file.
void close();
None.
NS_ERROR_NOT_INITIALIZED
NS_ERROR_IN_PROGRESS
Other errors may be thrown if a failure to complete the Zip file occurs.
Returns a specified entry in the current Zip file.
nsIZipEntry getEntry( in AUTF8String aZipEntry, );
aZipEntry
An nsIZipEntry
object describing the specified entry, or null
if there is no such entry.
Determines whether or not a specific entry exists in the Zip file.
boolean hasEntry( in AUTF8String aZipEntry, );
aZipEntry
true
if there is an entry with the given path in the Zip file, otherwise returns false
.
Opens the specified Zip file.
void open( in nsIFile aFile, in PRInt32 aIoFlags );
aFile
aIoFlags
prio.h
.NS_ERROR_ALREADY_INITIALIZED
NS_ERROR_INVALID_ARG
aFile
parameter is null.NS_ERROR_FILE_NOT_FOUND
NS_ERROR_FILE_CORRUPTED
Other errors may be thrown upon failing to open the file, such as if the file is corrupt or in an unsupported format.
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.
void processQueue( in nsIRequestObserver aObserver, in nsISupports aContext );
aObserver
aContext
NS_ERROR_NOT_INITIALIZED
NS_ERROR_IN_PROGRESS
Removes an entry from the Zip file.
void removeEntry( in AUTF8String aZipEntry, in boolean aQueue );
aZipEntry
aQueue
true
to place the remove operation into the queue, or false
to process it at once.NS_ERROR_NOT_INITIALIZED
NS_ERROR_IN_PROGRESS
NS_ERROR_FILE_NOT_FOUND
Other errors may occur if updating the Zip file fails.
var zipWriter = Components.Constructor("@mozilla.org/zipwriter;1", "nsIZipWriter"); var zipW = new zipWriter(); zipW.open(myZipFilePath, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); zipW.comment = "This is a comment."; zipW.close();
PR_RDWR
and friends are constants that are not in any interface (see Bug 433295), so for the code above to work you need something like:
const PR_RDONLY = 0x01; const PR_WRONLY = 0x02; const PR_RDWR = 0x04; const PR_CREATE_FILE = 0x08; const PR_APPEND = 0x10; const PR_TRUNCATE = 0x20; const PR_SYNC = 0x40; const PR_EXCL = 0x80;
See PR_Open Documentation or File I/O Snippets for details.
This code synchronously adds the file specified by the nsIFile
theFile
to the Zip archive.
var zipWriter = Components.Constructor("@mozilla.org/zipwriter;1", "nsIZipWriter"); var zipW = new zipWriter(); zipW.open(myZipFilePath, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); zipW.addEntryFile("Path/For/This/File/In/Zip Archive", Components.interfaces.nsIZipWriter.COMPRESSION_DEFAULT, theFile, false); zipW.close();
The argument nyZipFilePath
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.
For other examples, take a look at the unit tests in the source tree: