aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/Archives/ZipArchiveReader.h
blob: 74fa26b6fe15b3365b826103482dc5d636440073 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma once

#include "clickhouse_config.h"

#if USE_MINIZIP
#include <IO/Archives/IArchiveReader.h>
#include <mutex>
#include <vector>


namespace DB
{
class ReadBuffer;
class ReadBufferFromFileBase;
class SeekableReadBuffer;

/// Implementation of IArchiveReader for reading zip archives.
class ZipArchiveReader : public IArchiveReader
{
public:
    /// Constructs an archive's reader that will read from a file in the local filesystem.
    explicit ZipArchiveReader(const String & path_to_archive_);

    /// Constructs an archive's reader that will read by making a read buffer by using
    /// a specified function.
    ZipArchiveReader(const String & path_to_archive_, const ReadArchiveFunction & archive_read_function_, UInt64 archive_size_);

    ~ZipArchiveReader() override;

    const std::string & getPath() const override;

    /// Returns true if there is a specified file in the archive.
    bool fileExists(const String & filename) override;

    /// Returns the information about a file stored in the archive.
    FileInfo getFileInfo(const String & filename) override;

    /// Starts enumerating files in the archive.
    std::unique_ptr<FileEnumerator> firstFile() override;

    /// Starts reading a file from the archive. The function returns a read buffer,
    /// you can read that buffer to extract uncompressed data from the archive.
    /// Several read buffers can be used at the same time in parallel.
    std::unique_ptr<ReadBufferFromFileBase> readFile(const String & filename, bool throw_on_not_found) override;
    std::unique_ptr<ReadBufferFromFileBase> readFile(NameFilter filter, bool throw_on_not_found) override;

    /// It's possible to convert a file enumerator to a read buffer and vice versa.
    std::unique_ptr<ReadBufferFromFileBase> readFile(std::unique_ptr<FileEnumerator> enumerator) override;
    std::unique_ptr<FileEnumerator> nextFile(std::unique_ptr<ReadBuffer> read_buffer) override;

    std::vector<std::string> getAllFiles() override;
    std::vector<std::string> getAllFiles(NameFilter filter) override;

    /// Sets password used to decrypt the contents of the files in the archive.
    void setPassword(const String & password_) override;

private:
    class ReadBufferFromZipArchive;
    class FileEnumeratorImpl;
    class HandleHolder;
    using RawHandle = void *;

    void init();

    struct FileInfoImpl : public FileInfo
    {
        int compression_method;
    };

    HandleHolder acquireHandle();
    RawHandle acquireRawHandle();
    void releaseRawHandle(RawHandle handle_);

    void checkResult(int code) const;
    [[noreturn]] void showError(const String & message) const;

    const String path_to_archive;
    const ReadArchiveFunction archive_read_function;
    const UInt64 archive_size = 0;
    String password;
    std::vector<RawHandle> free_handles;
    mutable std::mutex mutex;
};

}

#endif