⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stafzipfile.cpp

📁 Software Testing Automation Framework (STAF)的开发代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************//* 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"// constructorSTAFZipFile::STAFZipFile(STAFHandlePtr h, FILE *file, STAFRC_t *rc,                          STAFString *result, int fileExist,                         long end, long start){    handle = h;    zf = file;        if (end == -1)    {        fseek(file, 0, SEEK_END);        endPos = ftell(file);    }    else    {        endPos = end;    }    if (start == -1)    {        fseek(file, 0, SEEK_SET);        startPos = ftell(file);    }    else    {        startPos = start;    }            newZipFile = (fileExist) ? 0 : 1;     util = new STAFZipUtil(handle);    centralDirPtr = new STAFZipCentralDir();    STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipFile::STAFZipFile_CP1")                     + " startPos [" + startPos + "]"                     + " endPos [" + endPos + "]"                     + " newZipFile [" + newZipFile + "]");    if (!newZipFile)    {        STAFTrace::trace(kSTAFTraceServiceResult,                         STAFString("STAFZipFile::STAFZipFile_CP2"));        // file exists, try to read in central dir data        if ((*rc = centralDirPtr->readInData(zf, (uLong)startPos,             (uLong)endPos, result)) == kSTAFOk)        {            STAFTrace::trace(kSTAFTraceServiceResult,                             STAFString("STAFZipFile::STAFZipFile_CP3"));            if ((*rc = readInData(result)) == kSTAFOk)            {                STAFTrace::trace(kSTAFTraceServiceResult,                             STAFString("STAFZipFile::STAFZipFile_CP3.5"));                // this is an existing zip archive                newZipFile = 0;            }        }    }}// zipping the fileSTAFRC_t STAFZipFile::zipFile(const char *pathname, int prefixlen,                         int recursive, STAFString *result){    STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipFile::zipFile_CP1")                     + " pathname ["                     + pathname                     + "] prefixlen ["                     + prefixlen                     + "] recursive ["                     + recursive                     + "]");    // check to make sure the pathname exists    STAFFSPath path(pathname);    if (!path.exists())    {        *result = STAFString("STAFZipFile::zipFile: ")                  + "File does not exist ["                  + pathname                  + "]\n";        return kSTAFDoesNotExist;    }    STAFRC_t rc;    // build localFileHeaderListNew    rc = zipDir(pathname, prefixlen, recursive, result);    // if zip dir was unsuccessful or local file header list is empty    if (rc != kSTAFOk || localFileHeaderListNew.empty())    {        if (rc != kSTAFOk)        {            return rc;        }        else        {            *result = STAFString("STAFZipFile::zipFile: ")                      + STAFString("No file entries read.");            return kZIPGeneralZipError;        }    }    std::vector<STAFZipLocalFileHeader*>::iterator i;    if (newZipFile)    {        // adding files to a new zip archive        for (i = localFileHeaderListNew.begin(); i != localFileHeaderListNew.end();             i++)        {            // save local file header into zip archive            if ((rc = (*i)->flush(zf, result)) != kSTAFOk)            {                break;            }            // add file header to central dir            centralDirPtr->addFileHeader(*i);        }    }    else    {        // appending files to existing zip file            // start appending local file header from the beginning of            // central dir            if (fseek(zf, centralDirPtr->getOffset(), SEEK_SET) != 0)            {                *result = STAFString("STAFZipFile::zipFile: ")                              + STAFString("Can't fseek central dir [")                              + centralDirPtr->getOffset()                              + STAFString("].\n");                return kZIPGeneralZipError;            }        // appending local file headers one by one        for (i = localFileHeaderListNew.begin(); i != localFileHeaderListNew.end();            i++)        {            STAFTrace::trace(kSTAFTraceServiceResult,                             STAFString("STAFZipFile::zipFile_CP7")                             + " (*i)->fileName ["                             + (*i)->fileName                             + "]");            // search if there is any existing entry            if (centralDirPtr->find((*i)->fileName) != NULL)            {                *result = STAFString("STAFZipFile::zipFile: ")                              + "File name exists in zip ["                              + (*i)->fileName                              + "].\n";                rc = kSTAFAlreadyExists;                break;            }            // save local file header into zip archive            if ((rc = (*i)->flush(zf, result)) != kSTAFOk)            {                break;            }            // append file header to central dir            centralDirPtr->addFileHeader(*i);        } // iterate all local file headers    } // newZipFile    // flush central dir to zip file    if (rc == kSTAFOk)    {        centralDirPtr->cder->centralDirOffset = ftell(zf);        rc = centralDirPtr->flush(zf, result);        STAFTrace::trace(kSTAFTraceServiceResult,                         STAFString("STAFZipFile::zipFile_CP11")                         + " rc ["                         + rc                         + "]");    }    return rc;}// zip directorySTAFRC_t STAFZipFile::zipDir(const char *pathname, int prefixlen,                             int recursive, STAFString *result){    char filename[MAXFILENAME] = "";    int i, rc = kSTAFOk;    STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipFile::zipDir_CP1")                     + " pathname ["                     + pathname                     + "] prefixlen ["                     + prefixlen                     + "] recursive ["                     + recursive                     + "]");    STAFString path(pathname);    STAFFSPath directoryPath(path);    STAFFSEntryPtr entry;    try    {        entry = directoryPath.getEntry();    }    catch (STAFBaseOSErrorException &e)    {        *result = STAFString("STAFZipFile:zipDir: ")                  + e.getText() + STAFString(": ")                  + e.getErrorCode()                  + "\nError on Source Directory: "                  + path;        return kSTAFDoesNotExist;    }    // if the value of path name is a file    if (entry->type() == kSTAFFSFile)    {        // this is a file        if(*(pathname + strlen(pathname) - 1) == '/'           || *(pathname + strlen(pathname) - 1) == '\\')        {            // invalid file name            *result = STAFString("STAFZipFile:zipDir: ")                  + "Invalid file name: ["                  + pathname                  + "].\n";            return kSTAFInvalidValue;        }        // create a local file header for the current file        STAFZipLocalFileHeader *lfh =             new STAFZipLocalFileHeader(pathname, prefixlen);        localFileHeaderListNew.push_back(lfh);        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipFile::zipDir_CP3"));    }    else if (entry->type() == kSTAFFSDirectory)    {        // this is a directory        if (*(pathname + strlen(pathname) - 1) != '/' && *(pathname +        strlen(pathname) - 1) != '\\')        {            // invalid directory name            *result = STAFString("STAFZipFile:zipDir: ")                  + "Invalid directory name: ["                  + pathname                  + "].\n";            return kSTAFInvalidValue;        }        //number of entries in the directory        i = 0;        unsigned int entryTypesUInt = kSTAFFSFile | kSTAFFSDirectory;        STAFFSEnumPtr entryEnum = entry->enumerate();        // zip the directory        for (; entryEnum->isValid(); entryEnum->next())        {            // increase number of entries by one            i++;            STAFString entryName;            entryName =             entryEnum->entry()->path().asString().replace(kUTF8_BSLASH, kUTF8_SLASH);            if (entryName.length() > MAXFILENAME - 1)            {                *result = STAFString("STAFZipFile:zipDir: ")                          + "File name length exceeds "                          + MAXFILENAME                          + " charactors: ["                          + entryName                          + "].\n";                return kSTAFInvalidValue;                            }                        util->convertSTAFStringBuffer(entryName.toCurrentCodePage(), filename);            STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipFile::zipDir_CP6")                     + " filename ["                     + filename                     + "]");            if(entryEnum->entry()->type() == kSTAFFSDirectory)            {                // it is a dir, add ending "/" to the pathname                char *p;                                p = (char*)filename;                                while (*p != '\0') p++;                                if (*(p-1) != '\\' && *(p-1) != '/')                {                    *p = '/';                    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -