二、文档的压缩和解压

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-22 18:45   247   0

背景:

由于项目的需要使用到文档的压缩和解压功能,期初打算使用zlib,发现其只是提供了底层的接口供使用,主要是用来对文字的压缩和解压。但是针对大文档的压缩和解压,以及多层文档结构需要自己重新造轮子。后来发现了zip utils的包,现再其基础上进行修改,目前满足自身对文件解压和压缩的使用。

参考链接:http://www.wischik.com/lu/programmer/zip_utils.html

1. 下载源码

链接:https://download.csdn.net/download/z345436330/12939726

#include <Windows.h>

#include "ZipUtils.h"

static TCHAR* char2TCHAR(const char* file) {
 auto iLength = MultiByteToWideChar(CP_ACP, 0, file, strlen(file) + 1, NULL, 0);
 TCHAR *tchar = new WCHAR[iLength];
 MultiByteToWideChar(CP_ACP, 0, file, strlen(file) + 1, tchar, iLength);
 return tchar;
}

static void DirToZip(const HZIP& hz, TCHAR* sourceDirPath, TCHAR* saveDirPath, bool bSaveFirstDir) {
 TCHAR szFind[MAX_PATH];
 
 lstrcpy(szFind, sourceDirPath);
 lstrcat(szFind, L"\\*.*");
 WIN32_FIND_DATA FindFileData;
 HANDLE hFind = FindFirstFile(szFind, &FindFileData);
 if (INVALID_HANDLE_VALUE == hFind)  return;

 while (TRUE)
 {

  if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   if (FindFileData.cFileName[0] != '.')
   {
    TCHAR saveNextFileDirPath[MAX_PATH];
    if (bSaveFirstDir) {
     lstrcpy(saveNextFileDirPath, saveDirPath);
     lstrcat(saveNextFileDirPath, L"\\");
     lstrcat(saveNextFileDirPath, FindFileData.cFileName);
    }
    else {
     lstrcpy(saveNextFileDirPath, FindFileData.cFileName);
    }
    

    TCHAR nextSourceDirPath[MAX_PATH];
    lstrcpy(nextSourceDirPath, sourceDirPath);
    lstrcat(nextSourceDirPath, L"\\");
    lstrcat(nextSourceDirPath, FindFileData.cFileName);

    //添加目录zip
    ZipAddFolder(hz, saveNextFileDirPath);
    DirToZip(hz, nextSourceDirPath, saveNextFileDirPath, true);
   }
  }
  else
  {
   TCHAR saveFilePath[MAX_PATH];
   if (bSaveFirstDir) {
    lstrcpy(saveFilePath, saveDirPath);
    lstrcat(saveFilePath, L"\\");
    lstrcat(saveFilePath, FindFileData.cFileName);
   }
   else {
    lstrcpy(saveFilePath, FindFileData.cFileName);
   }

   TCHAR sourceFilePath[MAX_PATH];
   lstrcpy(sourceFilePath, sourceDirPath);
   lstrcat(sourceFilePath, L"\\");
   lstrcat(sourceFilePath, FindFileData.cFileName);

   ZipAdd(hz, saveFilePath, sourceFilePath);
  }
  if (!FindNextFile(hFind, &FindFileData))
   break;
 }
 FindClose(hFind);
}

void DirToZip(char* sourceDirPath, char* zipName, bool bSaveFirstDir = true) {
 TCHAR* filePath = char2TCHAR(sourceDirPath);
 TCHAR* name = char2TCHAR(zipName);
 HZIP hz = CreateZip(name, 0);
 DirToZip(hz, filePath, filePath, bSaveFirstDir);
 CloseZip(hz);
 delete[] name;
 delete[] filePath;
}

void ZipToDir(char* sourceFilePath, char* saveDirPath) {
 TCHAR* sourceFileTCHAR = char2TCHAR(sourceFilePath);
 TCHAR* saveDirTCHAR = char2TCHAR(saveDirPath);
 HZIP hz = OpenZip(sourceFileTCHAR, 0);
 SetUnzipBaseDir(hz, saveDirTCHAR);
 ZIPENTRY ze; GetZipItem(hz, -1, &ze); int numitems = ze.index;
 for (int zi = 0; zi<numitems; zi++)
 {
  ZIPENTRY ze; GetZipItem(hz, zi, &ze);
  UnzipItem(hz, zi, ze.name);
 }
 CloseZip(hz);
 delete[] saveDirTCHAR;
 delete[] sourceFileTCHAR;
}

2. api介绍

// sourceFilePath 为被解压文件路径, saveDirPath为解压后文件保存的路径
void ZipToDir(char* sourceFilePath, char* saveDirPath);

// sourceDirPath需要压缩的文件夹路径, zipName压缩后文件的名字, bSaveFirstDir是否保存最外层的文件夹名
void DirToZip(char* sourceDirPath, char* zipName, bool bSaveFirstDir);

3. example

#include "ZipUtils.h"

int main(int argc, char*  args[]) {
 ZipToDir("test.zip", "zhy1");
 DirToZip("zhy1", "zhy.zip", false);
    return 0;
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP