nsISupports
Last changed in Gecko 10.0 (Firefox 10.0 / Thunderbird 10.0 / SeaMonkey 2.7)Implemented by: @mozilla.org/libjar/zip-reader;1. To create an instance, use:
var zipReader = Components.classes["@mozilla.org/libjar/zip-reader;1"]
.createInstance(Components.interfaces.nsIZipReader);
nsIZipReader has a code page problem; that is, in the ZIP specification, filenames are supposed to use 7-bit ASCII; however, most modern filesystems use 8 bit code pages, such as UTF-8. This can mismatch characters in the nsIZipReader API.Starting in Gecko 10.0 (Firefox 10.0 / Thunderbird 10.0 / SeaMonkey 2.7), the nsIZipReader API supports a limited 8 bit code page usage. All functions now pass the filenames (both in and out) as AUTF8String. So you can now read and extract what you wrote with nsIZipWriter. If you show filenames from the findEntries() result in the user interface, the character matching is only fine on UTF-8 ZIP archives.
void close(); |
void extract(in AUTF8String zipEntry, in nsIFile outFile); void extract(in string zipEntry, in nsIFile outFile); Obsolete since Gecko 10 |
nsIUTF8StringEnumerator findEntries(in AUTF8String aPattern); nsIUTF8StringEnumerator findEntries(in string aPattern); Obsolete since Gecko 10 |
nsIPrincipal getCertificatePrincipal(in AUTF8String aEntryName); nsIPrincipal getCertificatePrincipal(in string aEntryName); Obsolete since Gecko 10 |
nsIZipEntry getEntry(in AUTF8String zipEntry); nsIZipEntry getEntry(in string zipEntry); Obsolete since Gecko 10 |
nsIInputStream getInputStream(in AUTF8String zipEntry); nsIInputStream getInputStream(in string zipEntry); Obsolete since Gecko 10 |
nsIInputStream getInputStreamWithSpec(in AUTF8String aJarSpec, in AUTF8String zipEntry); nsIInputStream getInputStreamWithSpec(in AUTF8String aJarSpec, in string zipEntry); Obsolete since Gecko 10 |
boolean hasEntry(in AUTF8String zipEntry); |
void init(in nsIFile zipFile); Obsolete since Gecko 1.9 |
void open(in nsIFile zipFile); |
void openInner(in nsIZipReader zipReader, in AUTF8String zipEntry); void openInner(in nsIZipReader zipReader, in string zipEntry); Obsolete since Gecko 10 |
void test(in AUTF8String aEntryName); void test(in string aEntryName); Obsolete since Gecko 10 |
| Attribute | Type | Description |
file |
|
The file that represents the zip with which this zip reader was initialized. Read only. |
manifestEntriesCount |
PRUint32 |
The number of entries in the manifest. Read only. |
Closes a zip reader. Subsequent attempts to extract() files or read from its input stream will result in an error. Subsequent attempts to access a nsIZipEntry obtained from this zip reader will cause unspecified behavior.
void close();
None.
Example demonstrating this function: List Contents of XPI and Read File Contents
Extracts a zip entry into a local file specified by outFile. The entry must be stored in the zip in either uncompressed or DEFLATE-compressed format for the extraction to be successful. If the entry is a directory, the directory will be extracted non-recursively.
void extract( in AUTF8String zipEntry, in nsIFile outFile );
zipEntryoutFileReturns a string enumerator containing the matching entry names.
nsIUTF8StringEnumerator findEntries( in AUTF8String aPattern );
aPatternfile. Set this parameter to null (javascript) or EmptyCString() (c++) to get all entries; otherwise, use the following syntax:
An aPattern not conforming to this syntax has undefined behavior.
The nsIUTF8StringEnumerator containing the matching entry names.
NS_ERROR_ILLEGAL_VALUEaPattern values.Returns an object describing the entity which signed an entry. parseManifest must be called first. If aEntryName is an entry in the jar, getInputStream() must be called after parseManifest. If aEntryName is an external file which has meta-information stored in the jar, verifyExternalFile (not yet implemented) must be called before getPrincipal.
nsIPrincipal getCertificatePrincipal( in string aEntryName );
aEntryNameAn nsIPrincipal.
Example demonstrating this function: List Contents of XPI and Read File Contents
Returns a nsIZipEntry describing a specified zip entry.
nsIZipEntry getEntry( in AUTF8String zipEntry );
zipEntryThe nsIZipEntry for the given zip entry.
Example demonstrating this function: List Contents of XPI and Read File Contents
Returns an input stream containing the contents of the specified zip entry.
nsIInputStream getInputStream( in AUTF8String zipEntry );
zipEntryopen() the stream from.The nsIInputStream containing the contents of the specified zip entry.
Example demonstrating this function: List Contents of XPI and Read File Contents
Returns an input stream containing the contents of the specified zip entry. If the entry refers to a directory (ends with '/'), a directory stream is opened, otherwise the contents of the file entry is returned.
nsIInputStream getInputStreamWithSpec( in AUTF8String aJarSpec, in AUTF8String zipEntry );
aJarSpeczipEntryopen() the stream from.The nsIInputStream containing the contents of the specified zip entry.
Checks whether the zipfile contains an entry specified by zipEntry.
boolean hasEntry( in AUTF8String zipEntry );
zipEntryBoolean value, true if the zip entry exists, false is it does not exist.
Initializes a zip reader after construction.
void init( in nsIFile zipFile );
zipFileOpens a zip file for reading. It is allowed to open with another file, but it needs to be closed first with close().
init()"."void open( in nsIFile zipFile );
zipFileExample demonstrating this function: List Contents of XPI and Read File Contents
Opens a zip file inside a zip file for reading.
void openInner( in nsIZipReader zipReader, in AUTF8String zipEntry );
zipReaderzipEntryTests the integrity of the archive by performing a CRC check on each item expanded into memory. If an entry is specified the integrity of only that item is tested. If null (javascript) or EmptyCString() (c++) is passed in the integrity of all items in the archive are tested.
void test( in AUTF8String aEntryName );
aEntryNamenull for checking all entries in the archive.This example uses findEntries, getEntry, getInputStream, open, and close. It also uses nsIScriptableInputStream to read the stream returned by getInputStream. XPI files are ZIP files, just Firefox needs the extension to be XPI. Change "PortableTester@jetpack.xpi" to the name of a XPI file in your extensions folder. Then you can copy and paste this code in scratchpad to run it.
var zr = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance(Ci.nsIZipReader);
Cu.import('resource://gre/modules/osfile.jsm');
Cu.import('resource://gre/modules/FileUtils.jsm');
var reusableStreamInstance = Cc['@mozilla.org/scriptableinputstream;1'].createInstance(Ci.nsIScriptableInputStream);
var pathToXpiToRead = OS.Path.join(OS.Constants.Path.profileDir, 'extensions', 'PortableTester@jetpack.xpi');
var nsiFileXpi = new FileUtils.File(pathToXpiToRead);
//Services.ww.activeWindow.alert(pathToXpiToRead);
try {
zr.open(nsiFileXpi); //if file dne it throws here
var entries = zr.findEntries('*'); //we use asterik because we want EVERYTHING listed out
while (entries.hasMore()) {
var entryPointer = entries.getNext(); //just a string of "zip path" (this means path to file in zip, and it uses forward slashes remember)
var entry = zr.getEntry(entryPointer); // should return true on `entry instanceof Ci.nsIZipEntry`
console.log('entryPointer', entryPointer);
/* CONSOLE OUTPUT
* "entryPointer" "bootstrap.js" Scratchpad/1:18
*/
console.info('entry', entry);
/* CONSOLE OUTPUT
* "entry" XPCWrappedNative_NoHelper { QueryInterface: QueryInterface(), compression: Getter, size: Getter, realSize: Getter, CRC32: Getter, isDirectory: Getter, lastModifiedTime: Getter, isSynthetic: Getter, permissions: Getter, compression: 8 } Scratchpad/1:19
*/
if (!entry.isDirectory) {
var inputStream = zr.getInputStream(entryPointer);
reusableStreamInstance.init(inputStream);
var fileContents = reusableStreamInstance.read(entry.realSize);
console.log('contenst of file=', fileContents);
} else {
console.log('is directory, no stream to read');
}
}
} catch (ex) {
console.warn('exception occured = ', ex);
if (ex.name == 'NS_ERROR_FILE_NOT_FOUND') {
Services.ww.activeWindow.alert('XPI at path does not exist!\n\nPath = ' + pathToXpiToRead);
}
} finally {
zr.close();
console.log('zr closed');
}