📄 stafzip.cpp
字号:
{
result = STAFString("STAFZipFile::STAFZipFile: ")
+ STAFString("Can't create file [")
+ zipFile + STAFString("].\n");
rc = kZIPGeneralZipError;
}
}
// get number of entries for FILE or DIRECTORY option
unsigned int numFilesToAdd = parsedResult->optionTimes("FILE");
unsigned int numDirsToAdd = parsedResult->optionTimes("DIRECTORY");
// zipping the files
for (unsigned int i = 1; i <= numFilesToAdd && rc == kSTAFOk; i++)
{
// resolve variables
STAFString thisEntry = parsedResult->optionValue("FILE", i);
resultPtr = resolveStr(pInfo, pData, thisEntry);
if (resultPtr->rc != 0)
{
rc = resultPtr->rc;
result = resultPtr->result;
break;
}
thisEntry = resultPtr->result;
// replace all backward slashes with forward slashes
thisEntry = thisEntry.replace(kUTF8_BSLASH, kUTF8_SLASH);
entryLength = thisEntry.length();
if (entryLength <= MAXFILENAME)
{
util.convertSTAFStringBuffer(thisEntry.toCurrentCodePage(), entry);
}
else
{
result = STAFString("STAFZip::handleAdd: FILE [")
+ entry
+ "] file name length exceeds "
+ MAXFILENAME
+ " charactors.\n";
rc = kSTAFInvalidValue;
break;
}
if (prefixLength != 0)
{
// check if excludeprefix is shorter than entry name
if (prefixLength > strlen(entry))
{
result = STAFString("STAFZip::handleAdd: The length of"
" RELATIVETO [")
+ excludePrefix
+ "] exceeds the length of FILE ["
+ entry
+ "].\n";
rc = kSTAFInvalidValue;
break;
}
// save the char of entry at the prefixLength
char c = *(entry + prefixLength);
// truncate the entry to be a identical c string as excludeprefix
entry[prefixLength] = kUTF8_NULL;
// check if excludeprefix is a part of the entry name
if (util.myStrCmp(entry, excludePrefix))
{
result = STAFString("STAFZip::handleAdd: RELATIVETO [")
+ excludePrefix
+ "] is not a leading sub-string of FILE ["
+ entry
+ "].\n";
rc = kSTAFInvalidValue;
break;
}
// restore the char of entry at the prefixLength
*(entry + prefixLength) = c;
}
zf = new STAFZipFile(pData->fHandlePtr, file,
&rc, &resultBuffer, fileExist);
if (rc != kSTAFOk)
{
delete zf,
result = resultBuffer;
break;
}
if (STAFTrace::doTrace(kSTAFTraceServiceResult))
{
STAFTrace::trace(kSTAFTraceServiceResult,
STAFString("handleAdd_CP3")
+ " entry ["
+ entry
+ "] prefixLength ["
+ prefixLength
+ "] recursive ["
+ recursive
+ "]");
}
// adding the entry to the zip archive
if ((rc = zf->zipFile(entry, prefixLength, recursive, &resultBuffer)) !=
kSTAFOk)
{
result = resultBuffer;
}
delete zf;
}
// zipping the directories
for (unsigned int j = 1; j <= numDirsToAdd && rc == kSTAFOk; j++)
{
// resolve variables
STAFString thisEntry = parsedResult->optionValue("DIRECTORY", j);
resultPtr = resolveStr(pInfo, pData, thisEntry);
if (resultPtr->rc != 0)
{
rc = resultPtr->rc;
result = resultPtr->result;
break;
}
thisEntry = resultPtr->result;
// replace all backward slashes with forward slashes
thisEntry = thisEntry.replace(kUTF8_BSLASH, kUTF8_SLASH);
entryLength = thisEntry.length();
if (entryLength <= MAXFILENAME)
{
util.convertSTAFStringBuffer(thisEntry.toCurrentCodePage(), entry);
// append a '/' to the end of directory name
if (entry[entryLength - 1] != '/')
{
entry[entryLength] = '/';
entry[entryLength + 1] = kUTF8_NULL;
entryLength++;
}
}
else
{
result = STAFString("STAFZip::handleAdd: DIRECTORY [")
+ entry
+ "] file name length exceeds "
+ MAXFILENAME
+ " charactors.\n";
rc = kSTAFInvalidValue;
break;
}
if (prefixLength != 0)
{
// check if excludeprefix is shorter than entry name
if (prefixLength > strlen(entry))
{
result = STAFString("STAFZip::handleAdd: The length of"
" RELATIVETO [")
+ excludePrefix
+ "] exceeds the length of DIRECTORY ["
+ entry
+ "].\n";
rc = kSTAFInvalidValue;
break;
}
// save the char of entry at the prefixLength
char c = *(entry + prefixLength);
// truncate the entry to be a identical c string as excludeprefix
entry[prefixLength] = kUTF8_NULL;
// check if excludeprefix is a part of the entry name
if (util.myStrCmp(entry, excludePrefix))
{
result = STAFString("STAFZip::handleAdd: RELATIVETO [")
+ excludePrefix
+ "] is not a leading sub-string of DIRECTORY ["
+ entry
+ "].\n";
rc = kSTAFInvalidValue;
break;
}
// restore the char of entry at the prefixLength
*(entry + prefixLength) = c;
}
zf = new STAFZipFile(pData->fHandlePtr, file,
&rc, &resultBuffer, fileExist);
if (rc != kSTAFOk)
{
delete zf,
result = resultBuffer;
break;
}
if (STAFTrace::doTrace(kSTAFTraceServiceResult))
{
STAFTrace::trace(kSTAFTraceServiceResult,
STAFString("handleAdd_CP3")
+ " entry ["
+ entry
+ "] prefixLength ["
+ prefixLength
+ "] recursive ["
+ recursive
+ "]");
}
// adding the entry to the zip archive
if ((rc = zf->zipFile(entry, prefixLength, recursive, &resultBuffer)) !=
kSTAFOk)
{
result = resultBuffer;
}
delete zf;
}
// file operation is done, let's close the file handle
if (file != NULL)
{
fclose(file);
}
if (rc == kSTAFOk)
{
if (fileExist)
{
// if appending to existing archive
// remove the original zip archive
// since we are working on the backup copy
if (remove(zipFile) != 0)
{
result = STAFString("STAFZip::handleAdd: ")
+ STAFString("Remove file fails: [")
+ STAFString(zipFile)
+ STAFString("].\n");
rc = kSTAFFileDeleteError;
}
// rename the backup copy to be the zip archive name
if (rename(zipFileBackup, zipFile) != 0)
{
result = STAFString("STAFZip::handleAdd: ")
+ STAFString("Rename file fails from [")
+ STAFString(zipFileBackup)
+ STAFString("] to [")
+ STAFString(zipFile)
+ STAFString("].\n");
rc = kZIPGeneralZipError;
}
}
}
else
{
// if there was error occurred
if (fileExist)
{
// if appending to existing zip archive,
// remove the backup copy that we are working on
// so that leaves the original untouched
remove(zipFileBackup);
}
else
{
// if this is a new archive that we create
// just remove it since there was an error during zipping
remove(zipFile);
}
}
return STAFResultPtr(new STAFResult(rc, result),
STAFResultPtr::INIT);
}
// Handles Unzip requests
STAFResultPtr handleUnzip(STAFServiceRequestLevel30 *pInfo,
STAFZipServiceData *pData, STAFString *mutexName)
{
STAFString result;
STAFString resultBuffer;
char zipFile[MAXFILENAME + 1] = "";
char fileName[MAXFILENAME + 1] = "";
char toDir[MAXFILENAME + 1] = "";
unsigned int toDirLength = 0;
unsigned int fileLength = 0;
int replace = 0;
int restorePermission = 0;
STAFZipFile *zf;
STAFZipUtil util = STAFZipUtil(pData->fHandlePtr);
STAFRC_t rc = kSTAFOk;
FILE *file;
// Verify the requester has at least trust level 4
VALIDATE_TRUST(4, pData->fShortName, "UNZIP", pData->fLocalMachineName);
// Parse the request
STAFCommandParseResultPtr parsedResult = pData->fUnzipParser->parse(
pInfo->request);
if (parsedResult->rc != kSTAFOk)
{
return STAFResultPtr(new STAFResult(kSTAFInvalidRequestString,
parsedResult->errorBuffer), STAFResultPtr::INIT);
}
// get "ZIPFILE" parameter value
STAFResultPtr resultPtr = resolveOp(pInfo, pData, parsedResult, "ZIPFILE");
if (resultPtr->rc != 0) return resultPtr;
if (resultPtr->result.length() <= MAXFILENAME)
{
util.convertSTAFStringBuffer(resultPtr->result.toCurrentCodePage(), zipFile);
}
else
{
return STAFResultPtr(new STAFResult(kSTAFInvalidValue,
STAFString("STAFZip::handleUnzip:"
" ZIPFILE [")
+ zipFile
+ "] file name length exceeds "
+ MAXFILENAME
+ " charactors.\n"),
STAFResultPtr::INIT);
}
resultPtr->result = resultPtr->result.replace(kUTF8_BSLASH, kUTF8_SLASH);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -