aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/Archives/createArchiveReader.cpp
blob: 0c998971de1502e4ff63701c7bdbd9c1369adf58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <IO/Archives/createArchiveReader.h>
#include <IO/Archives/ZipArchiveReader.h>
#include <IO/Archives/LibArchiveReader.h>
#include <Common/Exception.h>


namespace DB
{
namespace ErrorCodes
{
    extern const int CANNOT_UNPACK_ARCHIVE;
    extern const int SUPPORT_IS_DISABLED;
}


std::shared_ptr<IArchiveReader> createArchiveReader(const String & path_to_archive)
{
    return createArchiveReader(path_to_archive, {}, 0);
}


std::shared_ptr<IArchiveReader> createArchiveReader(
    const String & path_to_archive,
    [[maybe_unused]] const std::function<std::unique_ptr<SeekableReadBuffer>()> & archive_read_function,
    [[maybe_unused]] size_t archive_size)
{
    using namespace std::literals;
    static constexpr std::array tar_extensions
    {
        ".tar"sv,
        ".tar.gz"sv,
        ".tgz"sv,
        ".tar.zst"sv,
        ".tzst"sv,
        ".tar.xz"sv,
        ".tar.bz2"sv
    };

    if (path_to_archive.ends_with(".zip") || path_to_archive.ends_with(".zipx"))
    {
#if USE_MINIZIP
        return std::make_shared<ZipArchiveReader>(path_to_archive, archive_read_function, archive_size);
#else
        throw Exception(ErrorCodes::SUPPORT_IS_DISABLED, "minizip library is disabled");
#endif
    }
    else if (std::any_of(
                 tar_extensions.begin(), tar_extensions.end(), [&](const auto extension) { return path_to_archive.ends_with(extension); }))
    {
#if USE_LIBARCHIVE
        return std::make_shared<TarArchiveReader>(path_to_archive);
#else
        throw Exception(ErrorCodes::SUPPORT_IS_DISABLED, "libarchive library is disabled");
#endif
    }
    else if (path_to_archive.ends_with(".7z"))
    {
#if USE_LIBARCHIVE
        return std::make_shared<SevenZipArchiveReader>(path_to_archive);
#else
        throw Exception(ErrorCodes::SUPPORT_IS_DISABLED, "libarchive library is disabled");
#endif
    }
    else
    {
        throw Exception(ErrorCodes::CANNOT_UNPACK_ARCHIVE, "Cannot determine the type of archive {}", path_to_archive);
    }
}

}