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

📄 stafziplocalfileheadercommon.cpp

📁 Software Testing Automation Framework (STAF)的开发代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
 /*****************************************************************************//* 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 <vector>#include <map>#include "zlib.h"#include "zutil.h"#include "STAFZip.h"#include "STAFZipUtil.h"#include "STAFZipFileAttribute.h"#include "STAFZipCentralDirExtension.h"#include "STAFZipLocalFileHeader.h"#include "STAFZipFileHeader.h"// constructorSTAFZipLocalFileHeader::STAFZipLocalFileHeader(){    signature = 0x04034b50;    // 2 bytes, zip 2.0    versionNeededToExtract = 20;    // 2 bytes, general purpose bit flag bit 2, 1 set to be 1 0 for    // maximum compression    generalPurposeBitFlag = 2;    // 2 bytes, method 8 - deflating    compressionMethod = Z_DEFLATED;    // 2 bytes time, 2 bytes date    lastModifiedTimeDate = 0;    // 4 bytes    crc = 0;    // 4 bytes, not initialized yet    compressedSize = 0;    // 4 bytes, not initialized yet    uncompressedSize = 0;    // 2 bytes    fileNameLength = 0;    // 2 bytes    extraFieldLength = 0;    fileName = NULL;    extraField = NULL;    fullFileName = NULL;    size = 30;    offset = 0;}// constructor based on pathname and prefix lengthSTAFZipLocalFileHeader::STAFZipLocalFileHeader(const char *pathname,                                               int prefixlen){    STAFZipUtil util = STAFZipUtil();    signature = 0x04034b50;    fileName = NULL;    fullFileName = NULL;    // 2 bytes    fileNameLength = 0;    // 2 bytes time, 2 bytes date    lastModifiedTimeDate = 0;    fullFileName = (char*)calloc(strlen(pathname) + 1, 1);    if (fullFileName != NULL)    {        strcpy(fullFileName, pathname);        fileName = util.calculateFileNameInZip(fullFileName, prefixlen);        fileNameLength = strlen(fileName);        lastModifiedTimeDate = util.fileTime(fullFileName);    }    else    {        STAFTrace::trace(kSTAFTraceServiceResult,             STAFString("STAFZipLocalFileHeader::STAFZipLocalFileHeader1_CP1")                     + "Error allocating memory for full file name.\n");    }    // 2 bytes, zip 2.0    versionNeededToExtract = 20;    // 2 bytes, general purpose bit flag bit 2, 1 set to be 1 0 for    // maximum compression    generalPurposeBitFlag = 2;    // 2 bytes, method 8 - deflating    compressionMethod = Z_DEFLATED;    // 4 bytes    crc = 0;    // 4 bytes, not initialized yet    compressedSize = 0;    // 4 bytes, not initialized yet    uncompressedSize = 0;    // 2 bytes    extraFieldLength = 0;    extraField = NULL;    size = 30 + fileNameLength;    offset = 0;}// flush local file header to zip archive and deflating data by// using of zlib utilitySTAFRC_t STAFZipLocalFileHeader::flush(FILE *zf, STAFString *result){    STAFZipUtil util = STAFZipUtil();    STAFRC_t rc;    int err;    // save the current file offset    offset = ftell(zf);    // write the local header    STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP1")                     + " offset ["                     + offset                     + "] signature ["                     + signature                     + "]");    // local header magic    rc = util.putValue(zf, (uLong)signature, 4);    // version needed to extract    if (rc == kSTAFOk)    {        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP2")                     + " versionNeededToExtract ["                     + versionNeededToExtract                     + "]");        rc = util.putValue(zf, (uLong)versionNeededToExtract, 2);    }    // general purpose bit flag, 2 for Maximum (-exx/-ex) compression option was    // used for method 8 and 9 - deflating    if (rc == kSTAFOk)    {        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP3")                     + " generalPurposeBitFlag ["                     + generalPurposeBitFlag                     + "]");        rc = util.putValue(zf, (uLong)generalPurposeBitFlag, 2);    }    // compression method    if (rc == kSTAFOk)    {        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP4")                     + " compressionMethod ["                     + compressionMethod                     + "]");        rc = util.putValue(zf, (uLong)compressionMethod, 2);    }    // file time in DOS format    if (rc == kSTAFOk)    {        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP5")                     + " lastModifiedTimeDate ["                     + lastModifiedTimeDate                     + "]");        rc = util.putValue(zf, (uLong)lastModifiedTimeDate, 4);    }    // CRC 32, unknown    if (rc == kSTAFOk)    {        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP6")                     + " crc ["                     + crc                     + "]");        rc = util.putValue(zf, (uLong)crc, 4);    }    // compressed size, unknown    if (rc == kSTAFOk)    {        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP7")                     + " compressedSize ["                     + compressedSize                     + "]");        rc = util.putValue(zf, (uLong)compressedSize, 4);    }    // uncompressed size, unknown    if (rc == kSTAFOk)    {        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP7")                     + " uncompressedSize ["                     + uncompressedSize                     + "]");        rc = util.putValue(zf, (uLong)uncompressedSize, 4);    }    // file name size    if (rc == kSTAFOk)    {        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP8")                     + " fileNameLength ["                     + fileNameLength                     + "]");        rc = util.putValue(zf, (uLong)fileNameLength, 2);    }    // extra field size    if (rc == kSTAFOk)    {        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP9")                     + " extraFieldLength ["                     + extraFieldLength                     + "]");        rc = util.putValue(zf, (uLong)extraFieldLength, 2);    }    // file name    if (rc == kSTAFOk && fileNameLength > 0)    {        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP10")                     + " fileName ["                     + fileName                     + "]");        if (fwrite(fileName, (uInt)fileNameLength, 1, zf)!=1)        {            *result = STAFString("STAFZipLocalFileHeader::flush: ")                          + "Error writting file name ["                          + fileName                          + "].\n";            rc = kZIPGeneralZipError;        }    }    // extra field    if (rc == kSTAFOk && extraFieldLength > 0)    {        STAFTrace::trace(kSTAFTraceServiceResult,                     STAFString("STAFZipLocalFileHeader::flush_CP12")                     + " extraField offset ["                     + ftell(zf)                     + "]");        if (fwrite(extraField, (uInt)extraFieldLength, 1, zf)!=1)        {            *result = STAFString("STAFZipLocalFileHeader::flush: ")                          + "Error writting extra field ["                          + fileName                          + "].\n";            rc = kZIPGeneralZipError;        }    }    if (rc != kSTAFOk)    {        if ((*result).length() == 0)        {            *result = STAFString("STAFZipLocalFileHeader::flush: ")                          + "Error writting data.\n";        }        return rc;    }    // start deflating the file    if (*(fileName + fileNameLength - 1) != '/'        && *(fileName + fileNameLength - 1) != '\\')    {        void *outbuf = NULL;        void *inbuf = NULL;        // allocate output buffer        outbuf = (void*)malloc(Z_BUFSIZE);        if(outbuf == NULL)        {            *result = STAFString("STAFZipLocalFileHeader::flush: ")                          + "Error allocating memory for zip output buffer ["                          + Z_BUFSIZE                          + "].\n";            return kZIPNotEnoughMemory;        }        // initialize zLib stream structure for deflate        z_stream stream;        stream.total_in = 0;        stream.total_out = 0;        stream.zalloc = (alloc_func)0;        stream.zfree = (free_func)0;        stream.opaque = (voidpf)0;        uInt level = 9;        err = deflateInit2(&stream, level,               Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0);        if (err != Z_OK)        {            free(outbuf);            *result = STAFString("STAFZipLocalFileHeader::flush: ")                          + "Error in zlib deflate init ["                          + err                          + "].\n";            return kZIPGeneralZipError;        }        // allocate input buffer        inbuf = (void*)malloc(Z_BUFSIZE);        if(inbuf == NULL)        {            *result = STAFString("STAFZipLocalFileHeader::flush: ")                          + "Error allocating memory for zip input buffer ["                          + Z_BUFSIZE                          + "].\n";            return kZIPNotEnoughMemory;        }        FILE *fin;        // open the to be zipped file        fin = fopen(fullFileName, "rb");        if (fin == NULL)        {            *result = STAFString("STAFZipLocalFileHeader::flush: ")                          + "Error in open file ["                          + fullFileName                          + "].\n";            free(inbuf);            free(outbuf);            return kSTAFFileOpenError;        }        uInt compressed_bytes;        stream.avail_in = (uInt)0;        // loop to read data from original file        while (err == Z_OK)        {            // prepare zip buffer

⌨️ 快捷键说明

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