/*
* Find an entry by name.
*/
ZipEntry* ZipFile::getEntryByName(const char* fileName) const
{
/*
* Do a stupid linear string-compare search.
*
* There are various ways to speed this up, especially since it's rare
* to intermingle changes to the archive with "get by name" calls. We
* don't want to sort the mEntries vector itself, however, because
* it's used to recreate the Central Directory.
*
* (Hash table works, parallel list of pointers in sorted order is good.)
*/
int idx;
for (idx = mEntries.size()-1; idx >= 0; idx--) {
ZipEntry* pEntry = mEntries[idx];
if (!pEntry->getDeleted() &&
strcmp(fileName, pEntry->getFileName()) == 0)
{
return pEntry;
}
}
return NULL;
}
/*
* Process a regular file, adding it to the archive if appropriate.
*
* This function is intended for use when creating a cached overlay package.
* Only xml and .9.png files are processed and added to the package.
*
* If we're in "update" mode, and the file already exists in the archive,
* delete the existing entry before adding the new one.
*/
bool processOverlayFile(Bundle* bundle, ZipFile* zip,
String8 storageName, const sp<const AaptFile>& file)
{
const bool hasData = file->hasData();
storageName.convertToResPath();
ZipEntry* entry;
bool fromGzip = false;
status_t result;
if (strcasecmp(storageName.getPathExtension().string(), ".gz") == 0) {
fromGzip = true;
storageName = storageName.getBasePath();
}
if (bundle->getUpdate()) {
entry = zip->getEntryByName(storageName.string());
if (entry != NULL) {
/* file already exists in archive; there can be only one */
if (entry->getMarked()) {
fprintf(stderr,
"ERROR: '%s' exists twice (check for with & w/o '.gz'?)\n",
file->getPrintableSource().string());
return false;
}
zip->remove(entry);
}
}
if (hasData) {
const char* name = storageName.string();
if (endsWith(name, ".9.png") || endsWith(name, ".xml") || endsWith(name, ".arsc")) {
result = zip->add(file->getData(), file->getSize(), storageName.string(),
file->getCompressionMethod(), &entry);
if (result == NO_ERROR) {
if (bundle->getVerbose()) {
printf(" '%s'%s", storageName.string(), fromGzip ? " (from .gz)" : "");
if (entry->getCompressionMethod() == ZipEntry::kCompressStored) {
printf(" (not compressed)\n");
} else {
printf(" (compressed %d%%)\n", calcPercent(entry->getUncompressedLen(),
entry->getCompressedLen()));
}
}
entry->setMarked(true);
} else {
if (result == ALREADY_EXISTS) {
fprintf(stderr, " Unable to add '%s': file already in archive (try '-u'?)\n",
file->getPrintableSource().string());
} else {
fprintf(stderr, " Unable to add '%s': Zip add failed\n",
file->getPrintableSource().string());
}
return false;
}
}
}
return true;
}
int ZipArchive::deleteEntry(const ZipEntry& entry) const {
if (!isOpen()) { return LIBZIPPP_ERROR_NOT_OPEN; }
if (entry.zipFile!=this) { return LIBZIPPP_ERROR_INVALID_ENTRY; }
if (mode==READ_ONLY) { return LIBZIPPP_ERROR_NOT_ALLOWED; } //deletion not allowed
if (entry.isFile()) {
int result = zip_delete(zipHandle, entry.getIndex());
if (result==0) { return 1; }
return LIBZIPPP_ERROR_UNKNOWN; //unable to delete the entry
} else {
int counter = 0;
vector<ZipEntry> allEntries = getEntries();
vector<ZipEntry>::const_iterator eit;
for(eit=allEntries.begin() ; eit!=allEntries.end() ; ++eit) {
ZipEntry ze = *eit;
int startPosition = ze.getName().find(entry.getName());
if (startPosition==0) {
int result = zip_delete(zipHandle, ze.getIndex());
if (result==0) { ++counter; }
else { return LIBZIPPP_ERROR_UNKNOWN; } //unable to remove the current entry
}
}
return counter;
}
}
//.........这里部分代码省略.........
fprintf(stderr, "ERROR: unable to process assets while packaging '%s'\n",
outputFile.string());
result = count;
goto bail;
}
if (bundle->getVerbose()) {
printf("Generated %d file%s\n", count, (count==1) ? "" : "s");
}
count = processJarFiles(bundle, zip);
if (count < 0) {
fprintf(stderr, "ERROR: unable to process jar files while packaging '%s'\n",
outputFile.string());
result = count;
goto bail;
}
if (bundle->getVerbose())
printf("Included %d file%s from jar/zip files.\n", count, (count==1) ? "" : "s");
result = NO_ERROR;
/*
* Check for cruft. We set the "marked" flag on all entries we created
* or decided not to update. If the entry isn't already slated for
* deletion, remove it now.
*/
{
if (bundle->getVerbose())
printf("Checking for deleted files\n");
int i, removed = 0;
for (i = 0; i < zip->getNumEntries(); i++) {
ZipEntry* entry = zip->getEntryByIndex(i);
if (!entry->getMarked() && entry->getDeleted()) {
if (bundle->getVerbose()) {
printf(" (removing crufty '%s')\n",
entry->getFileName());
}
zip->remove(entry);
removed++;
}
}
if (bundle->getVerbose() && removed > 0)
printf("Removed %d file%s\n", removed, (removed==1) ? "" : "s");
}
/* tell Zip lib to process deletions and other pending changes */
result = zip->flush();
if (result != NO_ERROR) {
fprintf(stderr, "ERROR: Zip flush failed, archive may be hosed\n");
goto bail;
}
/* anything here? */
if (zip->getNumEntries() == 0) {
if (bundle->getVerbose()) {
printf("Archive is empty -- removing %s\n", outputFile.getPathLeaf().string());
}
delete zip; // close the file so we can remove it in Win32
zip = NULL;
if (unlink(outputFile.string()) != 0) {
fprintf(stderr, "warning: could not unlink '%s'\n", outputFile.string());
}
}
/*
* Process a regular file, adding it to the archive if appropriate.
*
* If we're in "update" mode, and the file already exists in the archive,
* delete the existing entry before adding the new one.
*/
bool processFile(Bundle* bundle, ZipFile* zip,
String8 storageName, const sp<const AaptFile>& file)
{
const bool hasData = file->hasData();
ZipEntry* entry;
bool fromGzip = false;
status_t result;
/*
* See if the filename ends in ".EXCLUDE". We can't use
* String8::getPathExtension() because the length of what it considers
* to be an extension is capped.
*
* The Asset Manager doesn't check for ".EXCLUDE" in Zip archives,
* so there's no value in adding them (and it makes life easier on
* the AssetManager lib if we don't).
*
* NOTE: this restriction has been removed. If you're in this code, you
* should clean this up, but I'm in here getting rid of Path Name, and I
* don't want to make other potentially breaking changes --joeo
*/
int fileNameLen = storageName.length();
int excludeExtensionLen = strlen(kExcludeExtension);
if (fileNameLen > excludeExtensionLen
&& (0 == strcmp(storageName.string() + (fileNameLen - excludeExtensionLen),
kExcludeExtension))) {
fprintf(stderr, "warning: '%s' not added to Zip\n", storageName.string());
return true;
}
if (strcasecmp(storageName.getPathExtension().string(), ".gz") == 0) {
fromGzip = true;
storageName = storageName.getBasePath();
}
if (bundle->getUpdate()) {
entry = zip->getEntryByName(storageName.string());
if (entry != NULL) {
/* file already exists in archive; there can be only one */
if (entry->getMarked()) {
fprintf(stderr,
"ERROR: '%s' exists twice (check for with & w/o '.gz'?)\n",
file->getPrintableSource().string());
return false;
}
if (!hasData) {
const String8& srcName = file->getSourceFile();
time_t fileModWhen;
fileModWhen = getFileModDate(srcName.string());
if (fileModWhen == (time_t) -1) { // file existence tested earlier,
return false; // not expecting an error here
}
if (fileModWhen > entry->getModWhen()) {
// mark as deleted so add() will succeed
if (bundle->getVerbose()) {
printf(" (removing old '%s')\n", storageName.string());
}
zip->remove(entry);
} else {
// version in archive is newer
if (bundle->getVerbose()) {
printf(" (not updating '%s')\n", storageName.string());
}
entry->setMarked(true);
return true;
}
} else {
// Generated files are always replaced.
zip->remove(entry);
}
}
}
//android_setMinPriority(NULL, ANDROID_LOG_VERBOSE);
if (fromGzip) {
result = zip->addGzip(file->getSourceFile().string(), storageName.string(), &entry);
} else if (!hasData) {
/* don't compress certain files, e.g. PNGs */
int compressionMethod = bundle->getCompressionMethod();
if (!okayToCompress(bundle, storageName)) {
compressionMethod = ZipEntry::kCompressStored;
}
result = zip->add(file->getSourceFile().string(), storageName.string(), compressionMethod,
&entry);
} else {
result = zip->add(file->getData(), file->getSize(), storageName.string(),
file->getCompressionMethod(), &entry);
}
if (result == NO_ERROR) {
if (bundle->getVerbose()) {
//.........这里部分代码省略.........
/*
* Add an entry by copying it from another zip file, recompressing with
* Zopfli if already compressed.
*
* If "ppEntry" is non-NULL, a pointer to the new entry will be returned.
*/
status_t ZipFile::addRecompress(const ZipFile* pSourceZip, const ZipEntry* pSourceEntry,
ZipEntry** ppEntry)
{
ZipEntry* pEntry = NULL;
status_t result;
long lfhPosn, startPosn, endPosn, uncompressedLen;
if (mReadOnly)
return INVALID_OPERATION;
/* make sure we're in a reasonable state */
assert(mZipFp != NULL);
assert(mEntries.size() == mEOCD.mTotalNumEntries);
if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
result = UNKNOWN_ERROR;
goto bail;
}
pEntry = new ZipEntry;
if (pEntry == NULL) {
result = NO_MEMORY;
goto bail;
}
result = pEntry->initFromExternal(pSourceEntry);
if (result != NO_ERROR)
goto bail;
/*
* From here on out, failures are more interesting.
*/
mNeedCDRewrite = true;
/*
* Write the LFH, even though it's still mostly blank. We need it
* as a place-holder. In theory the LFH isn't necessary, but in
* practice some utilities demand it.
*/
lfhPosn = ftell(mZipFp);
pEntry->mLFH.write(mZipFp);
startPosn = ftell(mZipFp);
/*
* Copy the data over.
*
* If the "has data descriptor" flag is set, we want to copy the DD
* fields as well. This is a fixed-size area immediately following
* the data.
*/
if (fseek(pSourceZip->mZipFp, pSourceEntry->getFileOffset(), SEEK_SET) != 0)
{
result = UNKNOWN_ERROR;
goto bail;
}
uncompressedLen = pSourceEntry->getUncompressedLen();
if (pSourceEntry->isCompressed()) {
void *buf = pSourceZip->uncompress(pSourceEntry);
if (buf == NULL) {
result = NO_MEMORY;
goto bail;
}
long startPosn = ftell(mZipFp);
uint32_t crc;
if (compressFpToFp(mZipFp, NULL, buf, uncompressedLen, &crc) != NO_ERROR) {
ALOGW("recompress of '%s' failed\n", pEntry->mCDE.mFileName);
result = UNKNOWN_ERROR;
free(buf);
goto bail;
}
long endPosn = ftell(mZipFp);
pEntry->setDataInfo(uncompressedLen, endPosn - startPosn,
pSourceEntry->getCRC32(), ZipEntry::kCompressDeflated);
free(buf);
} else {
off_t copyLen;
copyLen = pSourceEntry->getCompressedLen();
if ((pSourceEntry->mLFH.mGPBitFlag & ZipEntry::kUsesDataDescr) != 0)
copyLen += ZipEntry::kDataDescriptorLen;
if (copyPartialFpToFp(mZipFp, pSourceZip->mZipFp, copyLen, NULL)
!= NO_ERROR)
{
ALOGW("copy of '%s' failed\n", pEntry->mCDE.mFileName);
result = UNKNOWN_ERROR;
goto bail;
}
}
/*
* Update file offsets.
*/
//.........这里部分代码省略.........
/*
* Add an entry by copying it from another zip file. If "padding" is
* nonzero, the specified number of bytes will be added to the "extra"
* field in the header.
*
* If "ppEntry" is non-NULL, a pointer to the new entry will be returned.
*/
status_t ZipFile::add(const ZipFile* pSourceZip, const ZipEntry* pSourceEntry,
int padding, ZipEntry** ppEntry)
{
ZipEntry* pEntry = NULL;
status_t result;
long lfhPosn, endPosn;
if (mReadOnly)
return INVALID_OPERATION;
/* make sure we're in a reasonable state */
assert(mZipFp != NULL);
assert(mEntries.size() == mEOCD.mTotalNumEntries);
if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
result = UNKNOWN_ERROR;
goto bail;
}
pEntry = new ZipEntry;
if (pEntry == NULL) {
result = NO_MEMORY;
goto bail;
}
result = pEntry->initFromExternal(pSourceZip, pSourceEntry);
if (result != NO_ERROR)
goto bail;
if (padding != 0) {
result = pEntry->addPadding(padding);
if (result != NO_ERROR)
goto bail;
}
/*
* From here on out, failures are more interesting.
*/
mNeedCDRewrite = true;
/*
* Write the LFH. Since we're not recompressing the data, we already
* have all of the fields filled out.
*/
lfhPosn = ftell(mZipFp);
pEntry->mLFH.write(mZipFp);
/*
* Copy the data over.
*
* If the "has data descriptor" flag is set, we want to copy the DD
* fields as well. This is a fixed-size area immediately following
* the data.
*/
if (fseek(pSourceZip->mZipFp, pSourceEntry->getFileOffset(), SEEK_SET) != 0)
{
result = UNKNOWN_ERROR;
goto bail;
}
off_t copyLen;
copyLen = pSourceEntry->getCompressedLen();
if ((pSourceEntry->mLFH.mGPBitFlag & ZipEntry::kUsesDataDescr) != 0)
copyLen += ZipEntry::kDataDescriptorLen;
if (copyPartialFpToFp(mZipFp, pSourceZip->mZipFp, copyLen, NULL)
!= NO_ERROR)
{
LOGW("copy of '%s' failed\n", pEntry->mCDE.mFileName);
result = UNKNOWN_ERROR;
goto bail;
}
/*
* Update file offsets.
*/
endPosn = ftell(mZipFp);
/*
* Success! Fill out new values.
*/
pEntry->setLFHOffset(lfhPosn); // sets mCDE.mLocalHeaderRelOffset
mEOCD.mNumEntries++;
mEOCD.mTotalNumEntries++;
mEOCD.mCentralDirSize = 0; // mark invalid; set by flush()
mEOCD.mCentralDirOffset = endPosn;
/*
* Add pEntry to the list.
*/
mEntries.add(pEntry);
if (ppEntry != NULL)
*ppEntry = pEntry;
pEntry = NULL;
//.........这里部分代码省略.........
/*
* Find the central directory and read the contents.
*
* The fun thing about ZIP archives is that they may or may not be
* readable from start to end. In some cases, notably for archives
* that were written to stdout, the only length information is in the
* central directory at the end of the file.
*
* Of course, the central directory can be followed by a variable-length
* comment field, so we have to scan through it backwards. The comment
* is at most 64K, plus we have 18 bytes for the end-of-central-dir stuff
* itself, plus apparently sometimes people throw random junk on the end
* just for the fun of it.
*
* This is all a little wobbly. If the wrong value ends up in the EOCD
* area, we're hosed. This appears to be the way that everbody handles
* it though, so we're in pretty good company if this fails.
*/
status_t ZipFile::readCentralDir(void)
{
status_t result = NO_ERROR;
unsigned char* buf = NULL;
off_t fileLength, seekStart;
long readAmount;
int i;
fseek(mZipFp, 0, SEEK_END);
fileLength = ftell(mZipFp);
rewind(mZipFp);
/* too small to be a ZIP archive? */
if (fileLength < EndOfCentralDir::kEOCDLen) {
LOGD("Length is %ld -- too small\n", (long)fileLength);
result = INVALID_OPERATION;
goto bail;
}
buf = new unsigned char[EndOfCentralDir::kMaxEOCDSearch];
if (buf == NULL) {
LOGD("Failure allocating %d bytes for EOCD search",
EndOfCentralDir::kMaxEOCDSearch);
result = NO_MEMORY;
goto bail;
}
if (fileLength > EndOfCentralDir::kMaxEOCDSearch) {
seekStart = fileLength - EndOfCentralDir::kMaxEOCDSearch;
readAmount = EndOfCentralDir::kMaxEOCDSearch;
} else {
seekStart = 0;
readAmount = (long) fileLength;
}
if (fseek(mZipFp, seekStart, SEEK_SET) != 0) {
LOGD("Failure seeking to end of zip at %ld", (long) seekStart);
result = UNKNOWN_ERROR;
goto bail;
}
/* read the last part of the file into the buffer */
if (fread(buf, 1, readAmount, mZipFp) != (size_t) readAmount) {
LOGD("short file? wanted %ld\n", readAmount);
result = UNKNOWN_ERROR;
goto bail;
}
/* find the end-of-central-dir magic */
for (i = readAmount - 4; i >= 0; i--) {
if (buf[i] == 0x50 &&
ZipEntry::getLongLE(&buf[i]) == EndOfCentralDir::kSignature)
{
LOGV("+++ Found EOCD at buf+%d\n", i);
break;
}
}
if (i < 0) {
LOGD("EOCD not found, not Zip\n");
result = INVALID_OPERATION;
goto bail;
}
/* extract eocd values */
result = mEOCD.readBuf(buf + i, readAmount - i);
if (result != NO_ERROR) {
LOGD("Failure reading %ld bytes of EOCD values", readAmount - i);
goto bail;
}
//mEOCD.dump();
if (mEOCD.mDiskNumber != 0 || mEOCD.mDiskWithCentralDir != 0 ||
mEOCD.mNumEntries != mEOCD.mTotalNumEntries)
{
LOGD("Archive spanning not supported\n");
result = INVALID_OPERATION;
goto bail;
}
/*
* So far so good. "mCentralDirSize" is the size in bytes of the
* central directory, so we can just seek back that far to find it.
* We can also seek forward mCentralDirOffset bytes from the
//.........这里部分代码省略.........
/*
* The directory hierarchy looks like this:
* "outputDir" and "assetRoot" are existing directories.
*
* On success, "bundle->numPackages" will be the number of Zip packages
* we created.
*/
status_t writeAPK(Bundle* bundle, ZipFile* zip, const char* outputFileName,
const sp<OutputSet>& outputSet, bool isOverlay)
{
status_t result = NO_ERROR;
int count;
if (bundle->getVerbose()) {
printf("Writing all files...\n");
}
count = processAssets(bundle, zip, outputSet, isOverlay);
if (count < 0) {
fprintf(stderr, "ERROR: unable to process assets while packaging '%s'\n",
outputFileName);
result = count;
goto bail;
}
if (bundle->getVerbose()) {
printf("Generated %d file%s\n", count, (count==1) ? "" : "s");
}
if (!isOverlay) {
count = processJarFiles(bundle, zip);
if (count < 0) {
fprintf(stderr, "ERROR: unable to process jar files while packaging '%s'\n",
outputFileName);
result = count;
goto bail;
}
if (bundle->getVerbose())
printf("Included %d file%s from jar/zip files.\n", count, (count==1) ? "" : "s");
}
result = NO_ERROR;
/*
* Check for cruft. We set the "marked" flag on all entries we created
* or decided not to update. If the entry isn't already slated for
* deletion, remove it now.
*/
{
if (bundle->getVerbose())
printf("Checking for deleted files\n");
int i, removed = 0;
for (i = 0; i < zip->getNumEntries(); i++) {
ZipEntry* entry = zip->getEntryByIndex(i);
if (!entry->getMarked() && entry->getDeleted()) {
if (bundle->getVerbose()) {
printf(" (removing crufty '%s')\n",
entry->getFileName());
}
zip->remove(entry);
removed++;
}
}
if (bundle->getVerbose() && removed > 0)
printf("Removed %d file%s\n", removed, (removed==1) ? "" : "s");
}
/* tell Zip lib to process deletions and other pending changes */
result = zip->flush();
if (result != NO_ERROR) {
fprintf(stderr, "ERROR: Zip flush failed, archive may be hosed\n");
goto bail;
}
bail:
return result;
}
/*
* The directory hierarchy looks like this:
* "outputDir" and "assetRoot" are existing directories.
*
* On success, "bundle->numPackages" will be the number of Zip packages
* we created.
*/
status_t writeAPK(Bundle* bundle, const sp<AaptAssets>& assets,
const String8& outputFile)
{
status_t result = NO_ERROR;
ZipFile* zip = NULL;
int count;
//bundle->setPackageCount(0);
/*
* Prep the Zip archive.
*
* If the file already exists, fail unless "update" or "force" is set.
* If "update" is set, update the contents of the existing archive.
* Else, if "force" is set, remove the existing archive.
*/
FileType fileType = getFileType(outputFile.string());
if (fileType == kFileTypeNonexistent) {
// okay, create it below
} else if (fileType == kFileTypeRegular) {
if (bundle->getUpdate()) {
// okay, open it below
} else if (bundle->getForce()) {
if (unlink(outputFile.string()) != 0) {
fprintf(stderr, "ERROR: unable to remove '%s': %s\n", outputFile.string(),
strerror(errno));
goto bail;
}
} else {
fprintf(stderr, "ERROR: '%s' exists (use '-f' to force overwrite)\n",
outputFile.string());
goto bail;
}
} else {
fprintf(stderr, "ERROR: '%s' exists and is not a regular file\n", outputFile.string());
goto bail;
}
if (bundle->getVerbose()) {
printf("%s '%s'\n", (fileType == kFileTypeNonexistent) ? "Creating" : "Opening",
outputFile.string());
}
status_t status;
zip = new ZipFile;
status = zip->open(outputFile.string(), ZipFile::kOpenReadWrite | ZipFile::kOpenCreate);
if (status != NO_ERROR) {
fprintf(stderr, "ERROR: unable to open '%s' as Zip file for writing\n",
outputFile.string());
goto bail;
}
if (bundle->getVerbose()) {
printf("Writing all files...\n");
}
count = processAssets(bundle, zip, assets);
if (count < 0) {
fprintf(stderr, "ERROR: unable to process assets while packaging '%s'\n",
outputFile.string());
result = count;
goto bail;
}
if (bundle->getVerbose()) {
printf("Generated %d file%s\n", count, (count==1) ? "" : "s");
}
count = processJarFiles(bundle, zip);
if (count < 0) {
fprintf(stderr, "ERROR: unable to process jar files while packaging '%s'\n",
outputFile.string());
result = count;
goto bail;
}
if (bundle->getVerbose())
printf("Included %d file%s from jar/zip files.\n", count, (count==1) ? "" : "s");
result = NO_ERROR;
/*
* Check for cruft. We set the "marked" flag on all entries we created
* or decided not to update. If the entry isn't already slated for
* deletion, remove it now.
*/
{
if (bundle->getVerbose())
printf("Checking for deleted files\n");
int i, removed = 0;
for (i = 0; i < zip->getNumEntries(); i++) {
ZipEntry* entry = zip->getEntryByIndex(i);
//.........这里部分代码省略.........
int ZipArchive::renameEntry(const ZipEntry& entry, const string& newName) const {
if (!isOpen()) { return LIBZIPPP_ERROR_NOT_OPEN; }
if (entry.zipFile!=this) { return LIBZIPPP_ERROR_INVALID_ENTRY; }
if (mode==READ_ONLY) { return LIBZIPPP_ERROR_NOT_ALLOWED; } //renaming not allowed
if (newName.length()==0) { return LIBZIPPP_ERROR_INVALID_PARAMETER; }
if (newName==entry.getName()) { return LIBZIPPP_ERROR_INVALID_PARAMETER; }
if (entry.isFile()) {
if (ENTRY_IS_DIRECTORY(newName)) { return LIBZIPPP_ERROR_INVALID_PARAMETER; } //invalid new name
int lastSlash = newName.rfind(ENTRY_PATH_SEPARATOR);
if (lastSlash!=1) {
bool dadded = addEntry(newName.substr(0, lastSlash+1));
if (!dadded) { return LIBZIPPP_ERROR_UNKNOWN; } //the hierarchy hasn't been created
}
int result = zip_file_rename(zipHandle, entry.getIndex(), newName.c_str(), ZIP_FL_ENC_GUESS);
if (result==0) { return 1; }
return LIBZIPPP_ERROR_UNKNOWN; //renaming was not possible (entry already exists ?)
} else {
if (!ENTRY_IS_DIRECTORY(newName)) { return LIBZIPPP_ERROR_INVALID_PARAMETER; } //invalid new name
int parentSlash = newName.rfind(ENTRY_PATH_SEPARATOR, newName.length()-2);
if (parentSlash!=-1) { //updates the dir hierarchy
string parent = newName.substr(0, parentSlash+1);
bool dadded = addEntry(parent);
if (!dadded) { return LIBZIPPP_ERROR_UNKNOWN; }
}
int counter = 0;
string originalName = entry.getName();
vector<ZipEntry> allEntries = getEntries();
vector<ZipEntry>::const_iterator eit;
for(eit=allEntries.begin() ; eit!=allEntries.end() ; ++eit) {
ZipEntry ze = *eit;
string currentName = ze.getName();
int startPosition = currentName.find(originalName);
if (startPosition==0) {
if (currentName == originalName) {
int result = zip_file_rename(zipHandle, entry.getIndex(), newName.c_str(), ZIP_FL_ENC_GUESS);
if (result==0) { ++counter; }
else { return LIBZIPPP_ERROR_UNKNOWN; } //unable to rename the folder
} else {
string targetName = currentName.replace(0, originalName.length(), newName);
int result = zip_file_rename(zipHandle, ze.getIndex(), targetName.c_str(), ZIP_FL_ENC_GUESS);
if (result==0) { ++counter; }
else { return LIBZIPPP_ERROR_UNKNOWN; } //unable to rename a sub-entry
}
} else {
//file not affected by the renaming
}
}
/*
* Special case for moving a directory a/x to a/x/y to avoid to lose
* the a/x path in the archive.
*/
bool newNameIsInsideCurrent = (newName.find(entry.getName())==0);
if (newNameIsInsideCurrent) {
bool dadded = addEntry(newName);
if (!dadded) { return LIBZIPPP_ERROR_UNKNOWN; }
}
return counter;
}
}
请发表评论