📄 stafzipfilecommon.cpp
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2004 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/#include "STAF.h"#include "STAFString.h"#include "STAFTrace.h"#include "STAFFileSystem.h"#include <sys/stat.h>#include <vector>#include <map>#include "STAFZip.h"#include "STAFZipUtil.h"#include "STAFZipFileHeader.h"#include "STAFZipFileAttribute.h"#include "STAFZipCentralDirExtension.h"#include "STAFZipLocalFileHeader.h"#include "STAFZipCentralDirEndRecord.h"#include "STAFZipCentralDir.h"#include "STAFZipFile.h"// Delete fileSTAFRC_t STAFZipFile::deleteFile(const char *filename, uLong *newsize, STAFString *result){ STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::deleteFile_CP1") + " filename [" + filename + "]"); if (newZipFile != 0) { *result = STAFString("STAFZipFile::deleteFile: ") + STAFString("Invalid format in zip archive"); return kZIPInvalidZipFile; } // get the file size uLong filesize = (uLong)endPos - startPos; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::deleteFile_CP4") + " filesize [" + filesize + "]"); STAFZipFileHeader *fh; STAFZipLocalFileHeader *lfh = find(filename); if (lfh == NULL) { *result = STAFString("STAFZipFile::deleteFile: ") + "File name not found in zip [" + filename + "].\n"; return kSTAFDoesNotExist; } // remove file header from central dir fh = centralDirPtr->remove(filename, lfh); // the last file in the zip archive if (centralDirPtr->getCentralDirEndRecord()->numberEntry <= 0) { // if this is the last file to be removed from zip archive // set the new size to be 0 *newsize = 0; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::deleteFile_CP6")); return kSTAFOk; } // remove local file header from zip file int c; fpos_t from, to; // move to the end of current local file header if (fseek(zf, lfh->offset + lfh->size, SEEK_SET) != 0) { *result = STAFString("STAFZipFile::deleteFile: ") + "Error in fseek to end of local header [" + (lfh->offset + lfh->size) + "].\n"; return kZIPGeneralZipError; } // set the from position fgetpos(zf, &from); // move to the beginning of current local file header if (fseek(zf, lfh->offset, SEEK_SET) != 0) { *result = STAFString("STAFZipFile::deleteFile: ") + "Error in fseek to beginning of local header [" + lfh->offset + "].\n"; return kZIPGeneralZipError; } // set the to position fgetpos(zf, &to); fsetpos(zf, &from); // move data from from position to to position while ((c = fgetc(zf))!= EOF) { fgetpos(zf, &from); fsetpos(zf, &to); fputc(c, zf); fgetpos(zf, &to); fsetpos(zf, &from); } // move to the beginning of central dir if (fseek(zf, centralDirPtr->getOffset(), SEEK_SET) != 0) { *result = STAFString("STAFZipFile::deleteFile: ") + "Error in fseek to beginning of central dir [" + centralDirPtr->getOffset() + "].\n"; return kZIPGeneralZipError; } // flush central dir STAFRC_t rc = centralDirPtr->flush(zf, result); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::deleteFile_CP10") + " rc [" + rc + "]"); if (rc == kSTAFOk) { *newsize = filesize - fh->size - lfh->size; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::deleteFile_CP11") + " *newsize [" + *newsize + "]"); } delete fh; return rc;}// list the content of zip archiveSTAFRC_t STAFZipFile::listFile(STAFString *buf, STAFString *result){ STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::listFile_CP1")); if (newZipFile != 0) { *result = STAFString("STAFZipFile::listFile: ") + STAFString("Invalid format in zip archive"); return kZIPInvalidZipFile; } return centralDirPtr->list(buf, result);}// Read in local file header data from Zip archiveSTAFRC_t STAFZipFile::readInData(STAFString *result){ uLong curPos = ftell(zf); STAFRC_t rc; std::vector<STAFZipFileHeader*>::iterator i; // get local file headers for (i = centralDirPtr->fileHeaderList.begin(); i != centralDirPtr->fileHeaderList.end(); i++) { STAFZipLocalFileHeader *lfh = new STAFZipLocalFileHeader(); lfh->offset = (*i)->localFileHeaderOffset; // move to the local file header if (fseek(zf, lfh->offset + startPos, SEEK_SET) != 0) { *result = STAFString("STAFZipFile::readInData: ") + STAFString("Error in fseek to local file header"); return kZIPGeneralZipError; } // get signature rc = util->getLong(zf, &lfh->signature); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::readInData_CP1") + " signature [" + lfh->signature + "]"); // get version needed to extract if (rc == kSTAFOk) { uLong iL; rc = util->getShort(zf, &iL); lfh->versionNeededToExtract = (unsigned short)iL; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::readInData_CP2") + " versionNeededToExtract [" + lfh->versionNeededToExtract + "]"); } // get general purpose bit flag if (rc == kSTAFOk) { uLong iL; rc = util->getShort(zf, &iL); lfh->generalPurposeBitFlag = (unsigned short)iL; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::readInData_CP3") + " generalPurposeBitFlag [" + lfh->generalPurposeBitFlag + "]"); } // get compression method if (rc == kSTAFOk) { uLong iL; rc = util->getShort(zf, &iL); lfh->compressionMethod = (unsigned short)iL; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipFile::readInData_CP4") + " compressionMethod [" + lfh->compressionMethod + "]"); } // get last mod time date if (rc == kSTAFOk) { rc = util->getLong(zf, &lfh->lastModifiedTimeDate);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -