📄 stafzipfile.cpp
字号:
} } else if (entryEnum->entry()->type() == kSTAFFSFile) { // it is a file, remove any possible ending "/" from the pathname char *p; p = (char*)filename; while (*p != '\0') p++; if (*(p-1) == '\\' || *(p-1) == '/') { *(p-1) = '\0'; } } // zip dir recursively if (recursive) { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::zipDir_CP8") + " filename [" + filename + "]"); // zip dir recursively if ((rc = zipDir(filename, prefixlen, recursive, result)) != kSTAFOk) { break; } } else { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::zipDir_CP10") + " filename [" + filename + "]"); // create local file header for the current file STAFZipLocalFileHeader *lfh = new STAFZipLocalFileHeader(filename, prefixlen); localFileHeaderListNew.push_back(lfh); // add file attribute of the current file if ((rc = cde->addFileAttribute(filename, prefixlen, result)) != kSTAFOk) { break; } } // if recurse } // loop to read dir entries STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::zipDir_CP12") + " filename [" + filename + "]"); // zip the empty dir if(i == 0 && rc == kSTAFOk) { STAFZipLocalFileHeader *lfh = new STAFZipLocalFileHeader(pathname, prefixlen); localFileHeaderListNew.push_back(lfh); rc = cde->addFileAttribute(pathname, prefixlen, result); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::zipDir_CP13") + " pathname [" + pathname + "] rc [" + rc + "]"); } } else { *result = STAFString("STAFZipFile:zipDir: ") + "Pathname is neither a file nor a directory: [" + pathname + "].\n"; return kZIPGeneralZipError; } return rc;}// unzip one fileSTAFRC_t STAFZipFile::unzipFile(const char *filename, char *outputdir, int replace, int restorepermission, STAFString *result){ STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile_CP1") + " filename [" + filename + "] outputdir [" + outputdir + "] replace [" + replace + "] restorepermission [" + restorepermission + "]"); int rc = kSTAFOk; char fullname[MAXFILENAME] = ""; // construct full file name strcpy(fullname, outputdir); strcat(fullname, filename); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile_CP3") + " fullname [" + fullname + "]"); // search central dir for file name STAFZipFileHeader *fh = centralDirPtr->find(filename); if (fh == NULL) { // Did not find the file in the zipfile *result = STAFString("STAFZipFile::unzipFile: ") + "File name not found in zip archive [" + filename + "].\n"; return kSTAFDoesNotExist; } // Found the file STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile_CP4") + " filename [" + filename + "]"); if (*(filename + strlen(filename) - 1) == '\\' || *(filename + strlen(filename) - 1) == '/') { // this is a directory STAFString path(fullname); STAFFSPath directoryPath(path); if (directoryPath.exists()) { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile_CP5")); // dir exists, do nothing return kSTAFOk; } } else { FILE *f; // this is a file, test if the file exists if ((f = fopen(fullname, "rb")) != NULL) { // file exists fclose(f); // if not replace if (!replace) { *result = STAFString("STAFZipFile::unzipFile: ") + "File already exists [" + fullname + "].\n"; return kSTAFAlreadyExists; } } } // find the local file header based on the file header STAFZipLocalFileHeader *lfh = find(fh->fileName); if (restorepermission) { // extract the file and restore permission rc = lfh->extract(zf, (uLong)startPos, outputdir, cde, fh, result); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile_CP7") + " rc [" + rc + "]"); } else { // extract the file, don't restore permission rc = lfh->extract(zf, (uLong)startPos, outputdir, result); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile_CP8") + " rc [" + rc + "]"); } return rc;}// unzip all files from zip archiveSTAFRC_t STAFZipFile::unzipFile(char *outputdir, int replace, int restorepermission, STAFString *result){ STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile2_CP1") + " outputdir [" + outputdir + "] replace [" + replace + "] restorepermission [" + restorepermission + "]"); STAFRC_t rc = kSTAFOk; char fullname[MAXFILENAME] = ""; std::vector<STAFZipLocalFileHeader*>::iterator i; // loop through all file headers in central dir for (i = localFileHeaderListCurrent.begin(); i != localFileHeaderListCurrent.end(); i++) { // construct full file name strcpy(fullname, outputdir); strcat(fullname, (*i)->fileName); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile2_CP3") + " fullname [" + fullname + "]"); if (*((*i)->fileName + (*i)->fileNameLength - 1) == '\\' || *((*i)->fileName + (*i)->fileNameLength - 1) == '/') { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile2_CP4")); // this is a directory STAFString path(fullname); STAFFSPath directoryPath(path); if (directoryPath.exists()) { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile2_CP5")); // dir exists, do nothing continue; } } else { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile2_CP6")); FILE *f; // this is a file, test tp see if the file exists if ((f = fopen(fullname, "rb")) != NULL) { // file exists fclose(f); if (!replace) { *result = STAFString("STAFZipFile::unzipFile2: ") + "File already exists [" + fullname + "].\n"; return kSTAFAlreadyExists; } } } if (restorepermission) { // search central dir for file name STAFZipFileHeader *fh = centralDirPtr->find((*i)->fileName); // extract file and restore permission rc = (*i)->extract(zf, (uLong)startPos, outputdir, cde, fh, result); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile2_CP8") + " rc [" + rc + "]"); } else { // extract file, don't restore permission rc = (*i)->extract(zf, (uLong)startPos, outputdir, result); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipFile2_CP9") + " rc [" + rc + "]"); } if (rc != kSTAFOk) { break; } } return rc;}// unzip dirSTAFRC_t STAFZipFile::unzipDir(const char *dirname, char *outputdir, int replace, int restorepermission, STAFString *result){ STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::unzipDir_CP1") + " dirname [" + dirname + "] outputdir [" + outputdir + "] replace [" + replace + "] restorepermission [" + restorepermission + "]"); int rc = kSTAFOk; std::vector<STAFString> nameList = findDir(dirname); if (nameList.size() >0) { std::vector<STAFString>::iterator i; for (i = nameList.begin(); i != nameList.end(); i++) { // unzip the file entry if ((rc = unzipFile(((*i)+STAFString(kUTF8_NULL)).buffer(), outputdir, replace, restorepermission, result)) != kSTAFOk) { // if fails, exit the loop break; } } } else { *result = STAFString("STAFZipFile::unzipDir_CP3: ") + STAFString("Directory ") + STAFString(dirname) + STAFString(" does not exist in zip archive"); rc = kSTAFDoesNotExist; } return rc;}// destructorSTAFZipFile::~STAFZipFile(){ STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::destructor_CP1")); std::vector<STAFZipLocalFileHeader*>::iterator i; for (i = localFileHeaderListNew.begin(); i != localFileHeaderListNew.end(); i++) { delete *i; } for (i = localFileHeaderListCurrent.begin(); i != localFileHeaderListCurrent.end(); i++) { delete *i; } delete centralDirPtr; delete util; delete cde;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -