bit7z压缩与解压

bit7z是一个对7-zip的静态wrapper库(GitHub - rikyoz/bit7z: A C++ static library offering a clean and simple interface to the 7-zip shared libraries. · GitHub),大大简化了文件压缩与解压的过程,提供一个简单的示例。7-zip支持市面上绝大多数压缩文件格式(zip,7z,tar,xz,...),都可以通过bit7z进行调用。

复制代码
#include <bit7z/bitarchivewriter.hpp>
#include <bit7z/bitarchivereader.hpp>
#include <io.h> 
#include <cstdio> 

#pragma comment(lib, "bit7z.lib")

class Bit7zHelper
{
public:
    Bit7zHelper() :total(0) {}
    void TotalSizeNotify(uint64_t sz)
    {
        total = sz;
        printf("TotalCallback: %lld.\n", sz);
    }
    bool ProgressUpdate(uint64_t sz)
    {
        printf("ProgressCallback: %.4f.\n", total == 0 ? 0.0 : (100 * 1.0 * sz) / total);
        return true;
    }
    void FileOnChange(bit7z::tstring filename)
    {
        printf("Begin Process File %s.\n",filename.c_str());
    }
private:
    uint64_t total;
};

bool CompressFolderTo()
{
    bool bRet = false;
    const char* dstFile = "C:/Users/project/test/output.7z";
    try
    { // bit7z classes can throw BitException objects
        using namespace bit7z;

        Bit7zLibrary lib{ "7z.dll" };//find 7z.dll on your computer,if you don't have 7z.dll, then install 7z software to get one.
        BitArchiveWriter archive{ lib, BitFormat::SevenZip };
        Bit7zHelper hlp;
        TotalCallback tcb = [&hlp](uint64_t sz) {hlp.TotalSizeNotify(sz); };
        FileCallback fcb= [&hlp](bit7z::tstring filename) {hlp.FileOnChange(filename); };
        ProgressCallback pcb= [&hlp](uint64_t sz) {return hlp.ProgressUpdate(sz); };
            
        archive.setTotalCallback(tcb);
        archive.setFileCallback(fcb);
        archive.setProgressCallback(pcb);
        archive.setPassword("123456");
        // Adding the items to be compressed (no compression is performed here)
        archive.addFile("C:/Users/project/test/file1.txt","A/B/file1.txt");
        archive.addFile("C:/Users/project/test/file2.txt", "A/file2.txt");
        //archive.addDirectory("C:/Users/project/test");//add the whole test folder(include the all subfolders and files of test)
        if (_access(dstFile, 0) == 0)//delete if already existed.
        {
            if (remove(dstFile) != 0) 
            {
                goto end;
            }
        }
        // Compressing the added items to the output archive
        archive.compressTo(dstFile);
        bRet = true;
    }
    catch (const bit7z::BitException& ex)
    { 
        std::error_code code = ex.code();
        int errCode = code.value();
        if (errCode == 126)
            printf("Please Get the 7z mudule firstly.\n");
        printf("error: %s[%d].\n", ex.what(), errCode);
    }
    end:
    return bRet;
}

bool ExtractCompressedTo()
{
    bool bRet = false;
    DWORD attr = 0;
    const char* srcZipfile = "C:/Users/project/test/output.7z";
    const char* dstFolder= "C:/Users/project";
    if (_access(dstFolder, 0) == -1)
        return bRet;
    if (_access(srcZipfile, 0) == -1)
        return bRet;
    attr = GetFileAttributesA(dstFolder);
    if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0)
        return bRet;
    try { // bit7z classes can throw BitException objects
        using namespace bit7z;

        Bit7zLibrary lib{ "7z.dll" };//find 7z.dll on your computer,if you don't have 7z.dll, then install 7z software to get one.

        // Opening the archive
        BitArchiveReader archive{ lib, srcZipfile, BitFormat::SevenZip };
        archive.setPassword("123456");
        // Testing the archive
        archive.test();
        
        // Extracting the archive
        archive.extractTo(dstFolder);
        bRet = true;
    }
    catch (const bit7z::BitException& ex)
    { 
        std::error_code code = ex.code();
        int errCode = code.value();
        if (errCode == 126)
            printf("Please Get the 7z mudule firstly.\n");
        printf("error: %s[%d].\n", ex.what(), errCode);
    }
    return bRet;
}
相关推荐
峥无12 小时前
C++11 深度详解:现代 C++ 基石全梳理
开发语言·c++·笔记
阿米亚波13 小时前
【C++ STL】std::unordered_multimap
开发语言·数据结构·c++·笔记·stl
小王C语言13 小时前
【3. 基于 Vibe Coding 的 OJ 平台】. 构建仓库、环境准备、需求梳理、安装依赖
网络·c++
小王C语言14 小时前
【8.进行接口测试】:通过 curl 进行接口自动化测试 / 通过 python 程序进行接口自动化测试
网络·c++
888CC++15 小时前
C++ 快速学习指南:从入门到进阶的实战路线
开发语言·c++
小小晓.15 小时前
C++记:函数
开发语言·c++·算法
驱动小百科16 小时前
Windows运行库合集下载 VC++、DirectX、.NET运行库安装教程
c++·windows·.net·windows运行库合集下载·windows运行库安装
charlie11451419116 小时前
Cinux —— 给物理内存建账本:bitmap 物理内存管理器
开发语言·c++·操作系统·开源项目
2301_7779983419 小时前
Linux线程同步与互斥(三):信号量与环形队列实现生产者消费者模型
linux·c语言·c++
啦啦啦啦啦zzzz19 小时前
设计模式:单例模式和工厂模式
c++·单例模式·设计模式·工厂模式