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; } }
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; } }