Esta página está traduciéndose a partir del artículo Creating_a Login Manager storage module, razón por la cual puede haber algunos errores sintácticos o partes sin traducir. Puedes colaborar continuando con la traducción
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);
Method overview
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) |
Attributes
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. |
Constants
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. |
Methods
addDirectoryEntry()
Adds a new directory entry to the Zip file.
void addEntryDirectory( in AUTF8String aZipEntry, in PRTime aModTime, in boolean aQueue );
Parameters
-
aZipEntry
- The path of the directory entry to add to the Zip file.
-
aModTime
- The modification time of the entry in microseconds.
-
aQueue
-
If
true
, the operation is queued for later execution. Iffalse
, the operation is performed immediately.
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.
addEntryChannel()
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 );
Parameters
-
aZipEntry
- The path of the file entry to add to the Zip file. This is the path it will be located at within the Zip file.
-
aModTime
- The modification time of the entry in microseconds.
-
aCompression
- One of the compression constants, indicating the compression method to use.
-
aChannel
- The channel from which to get the data.
-
aQueue
-
If
true
, the operation is queued for later execution. Iffalse
, the operation is performed immediately.
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:
addDirectoryEntry(aZipEntry, aFile.lastModifiedTime, aQueue);
void addEntryFile( in AUTF8String aZipEntry, in PRInt32 aCompression, in nsIFile aFile, in boolean aQueue );
Parameters
-
aZipEntry
- The path of the file entry to add to the Zip file. This is the path it will be located at within the Zip file.
-
aCompression
- One of the compression constants, indicating the compression method to use.
-
aFile
- The file from which to get the data and modification time.
-
aQueue
-
If
true
, the operation is queued for later execution. Iffalse
, the operation is performed immediately.
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.
addEntryStream()
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 );
Parameters
-
aZipEntry
- The path of the file entry to add to the Zip file. This is the path it will be located at within the Zip file.
-
aModTime
- The modification time of the entry in microseconds.
-
aCompression
- One of the compression constants, indicating the compression method to use.
-
aStream
- The input stream from which to get the data.
-
aQueue
-
If
true
, the operation is queued for later execution. Iffalse
, the operation is performed immediately.
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 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.
void processQueue( in nsIRequestObserver aObserver, in nsISupports aContext );
Parameters
-
aObserver
- The observer to receive notifications from the queue.
-
aContext
- The context to pass to the observer.
Exceptions thrown
-
NS_ERROR_NOT_INITIALIZED
- No Zip file is open.
-
NS_ERROR_IN_PROGRESS
- The queue is already in progress.
removeEntry()
Removes an entry from the Zip file.
void removeEntry( in AUTF8String aZipEntry, in boolean aQueue );
Parameters
-
aZipEntry
- The path of the entry to remove from the Zip file.
-
aQueue
-
true
to place the remove operation into the queue, orfalse
to process it at once.
Exceptions thrown
-
NS_ERROR_NOT_INITIALIZED
- No Zip file is open.
-
NS_ERROR_IN_PROGRESS
- The queue is already in progress.
-
NS_ERROR_FILE_NOT_FOUND
- No entry with the given path exists.
Other errors may occur if updating the Zip file fails.
Examples
Adding a comment to a Zip file
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.
Adding a file to a Zip archive
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.
Other examples
For other examples, take a look at the unit tests in the source tree: