[c++] 파일 압축 해제 옵션 설정

압축 파일 옵션 설정

파일을 압축 해제할 때 다양한 옵션을 설정할 수 있습니다. 예를 들어, 압축 해제 경로, 덮어쓰기 여부, 비밀번호 입력 등을 옵션으로 지정할 수 있습니다. 이를 통해 유연하게 파일을 압축 해제할 수 있습니다.

코드 예시

다음은 C++에서 파일 압축 해제 시 옵션을 설정하는 코드 예시입니다.

#include <iostream>
#include <fstream>
#include <vector>
#include <miniz.h>

void ExtractZipFile(const char* zipFilePath, const char* extractPath, bool overwrite, const char* password) {
    mz_zip_archive zip;
    memset(&zip, 0, sizeof(zip));
    mz_bool status = mz_zip_reader_init_file(&zip, zipFilePath);
    if (!status) {
        std::cout << "Failed to open the zip file" << std::endl;
        return;
    }

    // Set extraction options
    mz_zip_extract_options extractOptions = {};
    extractOptions.overwrite = overwrite ? 1 : 0;
    if (password != nullptr) {
        extractOptions.password = password;
    }

    // Extract the files
    mz_zip_extract_archive_file_to_heap(&zip, zip.file_index, extractPath, &extractOptions, nullptr, 0);
    mz_zip_reader_end(&zip);
}

int main() {
    const char* zipFilePath = "example.zip";
    const char* extractPath = "extracted_folder";
    bool overwrite = false;
    const char* password = nullptr;

    ExtractZipFile(zipFilePath, extractPath, overwrite, password);

    return 0;
}

위 코드에서는 ExtractZipFile 함수를 사용하여 압축 파일을 해제하고, 옵션을 설정할 수 있습니다. overwrite 매개변수에 따라 덮어쓰기 옵션을 설정하고, password 매개변수를 통해 비밀번호를 지정할 수 있습니다.

위 코드는 miniz 라이브러리를 사용하는 예시이며, 필요에 따라 해당 라이브러리를 프로젝트에 추가해야 합니다.

이제 파일의 압축 해제 과정에서 옵션을 설정하는 방법에 대해 이해할 수 있게 되었습니다.