/* void addEntryFile (in AUTF8String aZipEntry, in int32_t aCompression, * in nsIFile aFile, in boolean aQueue); */ NS_IMETHODIMP nsZipWriter::AddEntryFile(const nsACString & aZipEntry, int32_t aCompression, nsIFile *aFile, bool aQueue) { NS_ENSURE_ARG_POINTER(aFile); if (!mStream) return NS_ERROR_NOT_INITIALIZED; nsresult rv; if (aQueue) { nsZipQueueItem item; item.mOperation = OPERATION_ADD; item.mZipEntry = aZipEntry; item.mCompression = aCompression; rv = aFile->Clone(getter_AddRefs(item.mFile)); NS_ENSURE_SUCCESS(rv, rv); if (!mQueue.AppendElement(item)) return NS_ERROR_OUT_OF_MEMORY; return NS_OK; } if (mInQueue) return NS_ERROR_IN_PROGRESS; bool exists; rv = aFile->Exists(&exists); NS_ENSURE_SUCCESS(rv, rv); if (!exists) return NS_ERROR_FILE_NOT_FOUND; bool isdir; rv = aFile->IsDirectory(&isdir); NS_ENSURE_SUCCESS(rv, rv); PRTime modtime; rv = aFile->GetLastModifiedTime(&modtime); NS_ENSURE_SUCCESS(rv, rv); modtime *= PR_USEC_PER_MSEC; uint32_t permissions; rv = aFile->GetPermissions(&permissions); NS_ENSURE_SUCCESS(rv, rv); if (isdir) return InternalAddEntryDirectory(aZipEntry, modtime, permissions); if (mEntryHash.Get(aZipEntry, nullptr)) return NS_ERROR_FILE_ALREADY_EXISTS; nsCOMPtr<nsIInputStream> inputStream; rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream), aFile); NS_ENSURE_SUCCESS(rv, rv); rv = AddEntryStream(aZipEntry, modtime, aCompression, inputStream, false, permissions); NS_ENSURE_SUCCESS(rv, rv); return inputStream->Close(); }
/* void addEntryDirectory (in AUTF8String aZipEntry, in PRTime aModTime, * in boolean aQueue); */ NS_IMETHODIMP nsZipWriter::AddEntryDirectory(const nsACString & aZipEntry, PRTime aModTime, PRBool aQueue) { if (!mStream) return NS_ERROR_NOT_INITIALIZED; if (aQueue) { nsZipQueueItem item; item.mOperation = OPERATION_ADD; item.mZipEntry = aZipEntry; item.mModTime = aModTime; if (!mQueue.AppendElement(item)) return NS_ERROR_OUT_OF_MEMORY; return NS_OK; } if (mInQueue) return NS_ERROR_IN_PROGRESS; return InternalAddEntryDirectory(aZipEntry, aModTime); }
inline nsresult nsZipWriter::BeginProcessingAddition(nsZipQueueItem* aItem, bool* complete) { if (aItem->mFile) { bool exists; nsresult rv = aItem->mFile->Exists(&exists); NS_ENSURE_SUCCESS(rv, rv); if (!exists) return NS_ERROR_FILE_NOT_FOUND; bool isdir; rv = aItem->mFile->IsDirectory(&isdir); NS_ENSURE_SUCCESS(rv, rv); rv = aItem->mFile->GetLastModifiedTime(&aItem->mModTime); NS_ENSURE_SUCCESS(rv, rv); aItem->mModTime *= PR_USEC_PER_MSEC; rv = aItem->mFile->GetPermissions(&aItem->mPermissions); NS_ENSURE_SUCCESS(rv, rv); if (!isdir) { // Set up for fall through to stream reader rv = NS_NewLocalFileInputStream(getter_AddRefs(aItem->mStream), aItem->mFile); NS_ENSURE_SUCCESS(rv, rv); } // If a dir then this will fall through to the plain dir addition } uint32_t zipAttributes = ZIP_ATTRS(aItem->mPermissions, ZIP_ATTRS_FILE); if (aItem->mStream || aItem->mChannel) { nsRefPtr<nsZipHeader> header = new nsZipHeader(); NS_ENSURE_TRUE(header, NS_ERROR_OUT_OF_MEMORY); header->Init(aItem->mZipEntry, aItem->mModTime, zipAttributes, mCDSOffset); nsresult rv = header->WriteFileHeader(mStream); NS_ENSURE_SUCCESS(rv, rv); nsRefPtr<nsZipDataStream> stream = new nsZipDataStream(); NS_ENSURE_TRUE(stream, NS_ERROR_OUT_OF_MEMORY); rv = stream->Init(this, mStream, header, aItem->mCompression); NS_ENSURE_SUCCESS(rv, rv); if (aItem->mStream) { nsCOMPtr<nsIInputStreamPump> pump; rv = NS_NewInputStreamPump(getter_AddRefs(pump), aItem->mStream, -1, -1, 0, 0, true); NS_ENSURE_SUCCESS(rv, rv); rv = pump->AsyncRead(stream, nullptr); NS_ENSURE_SUCCESS(rv, rv); } else { rv = aItem->mChannel->AsyncOpen(stream, nullptr); NS_ENSURE_SUCCESS(rv, rv); } return NS_OK; } // Must be plain directory addition *complete = true; return InternalAddEntryDirectory(aItem->mZipEntry, aItem->mModTime, aItem->mPermissions); }