bit7z has eight main classes:

Examples

Below are a few examples that show how to use some of the main features of bit7z:

Extracting files from an archive

#include "bitextractor.hpp"

using namespace bit7z;

try {
    Bit7zLibrary lib{ L"7za.dll" };
    BitExtractor extractor{ lib, BitFormat::SevenZip };

    //extracting a simple archive
    extractor.extract( L"path/to/archive.7z", L"out/dir/" ); 

    //extracting a specific file
    extractor.extractMatching( L"path/to/arc.7z", L"file.pdf", L"out/dir/" ); 

    //extracting the first file of an archive to a buffer
    std::vector< byte_t > buffer;
    extractor.extract( L"path/to/archive.7z", buffer );

    //extracting an encrypted archive
    extractor.setPassword( L"password" );
    extractor.extract( L"path/to/another/archive.7z", L"out/dir/" );
} catch ( const BitException& ex ) {
    //do something with ex.what()...
}

Compressing files into an archive

#include "bitcompressor.hpp"

using namespace bit7z;

try {
    Bit7zLibrary lib{ L"7z.dll" };
    BitCompressor compressor{ lib, BitFormat::Zip };

    std::vector< std::wstring > files = { L"path/to/file1.jpg", L"path/to/file2.pdf" };

    //creating a simple zip archive
    compressor.compress( files, L"output_archive.zip" ); 

    //creating a zip archive with a custom directory structure
    std::map< std::wstring, std::wstring > files_map = { { L"path/to/file1.jpg",L"alias/path/file1.jpg" },
    { L"path/to/file2.pdf", L"alias/path/file2.pdf" } };
    compressor.compress( files_map, L"output_archive2.zip" );

    //compressing a directory
    compressor.compressDirectory( L"dir/path/", L"dir_archive.zip" ); 

    //creating an encrypted zip archive of two files
    compressor.setPassword( L"password" );
    compressor.compressFiles( files, L"protected_archive.zip" );

    //updating an existing zip archive
    compressor.setUpdateMode( true );
    compressor.compressFiles( files, L"existing_archive.zip" );

    //compressing a single file into a buffer
    std::vector< byte_t > buffer;
    BitCompressor compressor2{ lib, BitFormat::BZip2 };
    compressor2.compressFile( files[0], buffer );
} catch ( const BitException& ex ) {
    //do something with ex.what()...
}

Reading archive metadata

#include "bitarchiveinfo.hpp"

using namespace bit7z;

try {
    Bit7zLibrary lib{ L"7za.dll" };
    BitArchiveInfo arc{ lib, L"archive.7z", BitFormat::SevenZip };

    //printing archive metadata
    wcout << L"Archive properties" << endl;
    wcout << L" Items count: "   << arc.itemsCount() << endl;
    wcout << L" Folders count: " << arc.foldersCount() << endl;
    wcout << L" Files count: "   << arc.filesCount() << endl;
    wcout << L" Size: "          << arc.size() << endl;
    wcout << L" Packed size: "   << arc.packSize() << endl;
    wcout << endl;

    //printing archive items metadata
    wcout << L"Archive items";
    auto arc_items = arc.items();
    for ( auto& item : arc_items ) {
        wcout << endl;
        wcout << L" Item index: "   << item.index() << endl;
        wcout << L"  Name: "        << item.name() << endl;
        wcout << L"  Extension: "   << item.extension() << endl;
        wcout << L"  Path: "        << item.path() << endl;
        wcout << L"  IsDir: "       << item.isDir() << endl;
        wcout << L"  Size: "        << item.size() << endl;
        wcout << L"  Packed size: " << item.packSize() << endl;
    }
} catch ( const BitException& ex ) {
    //do something with ex.what()...
}





Add Discussion as Guest

Log in