blob: d996c4a3dfb832fec78f5770cd62ec2219598287 [file] [log] [blame]
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef LIBZIPARCHIVE_ZIPWRITER_H_
18#define LIBZIPARCHIVE_ZIPWRITER_H_
19
20#include "base/macros.h"
21#include <utils/Compat.h>
22
23#include <cstdio>
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070024#include <ctime>
Adam Lesinski591fd392015-10-06 15:23:46 -070025#include <memory>
26#include <string>
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070027#include <vector>
Adam Lesinski591fd392015-10-06 15:23:46 -070028#include <zlib.h>
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070029
30/**
31 * Writes a Zip file via a stateful interface.
32 *
33 * Example:
34 *
35 * FILE* file = fopen("path/to/zip.zip", "wb");
36 *
37 * ZipWriter writer(file);
38 *
39 * writer.StartEntry("test.txt", ZipWriter::kCompress | ZipWriter::kAlign);
40 * writer.WriteBytes(buffer, bufferLen);
41 * writer.WriteBytes(buffer2, bufferLen2);
42 * writer.FinishEntry();
43 *
44 * writer.StartEntry("empty.txt", 0);
45 * writer.FinishEntry();
46 *
47 * writer.Finish();
48 *
49 * fclose(file);
50 */
51class ZipWriter {
52public:
53 enum {
54 /**
55 * Flag to compress the zip entry using deflate.
56 */
57 kCompress = 0x01,
58
59 /**
60 * Flag to align the zip entry data on a 32bit boundary. Useful for
61 * mmapping the data at runtime.
62 */
63 kAlign32 = 0x02,
64 };
65
66 static const char* ErrorCodeString(int32_t error_code);
67
68 /**
69 * Create a ZipWriter that will write into a FILE stream. The file should be opened with
70 * open mode of "wb" or "w+b". ZipWriter does not take ownership of the file stream. The
71 * caller is responsible for closing the file.
72 */
73 explicit ZipWriter(FILE* f);
74
75 // Move constructor.
76 ZipWriter(ZipWriter&& zipWriter);
77
78 // Move assignment.
79 ZipWriter& operator=(ZipWriter&& zipWriter);
80
81 /**
82 * Starts a new zip entry with the given path and flags.
83 * Flags can be a bitwise OR of ZipWriter::kCompress and ZipWriter::kAlign.
84 * Subsequent calls to WriteBytes(const void*, size_t) will add data to this entry.
85 * Returns 0 on success, and an error value < 0 on failure.
86 */
87 int32_t StartEntry(const char* path, size_t flags);
88
89 /**
90 * Same as StartEntry(const char*, size_t), but sets a last modified time for the entry.
91 */
92 int32_t StartEntryWithTime(const char* path, size_t flags, time_t time);
93
94 /**
95 * Writes bytes to the zip file for the previously started zip entry.
96 * Returns 0 on success, and an error value < 0 on failure.
97 */
98 int32_t WriteBytes(const void* data, size_t len);
99
100 /**
101 * Finish a zip entry started with StartEntry(const char*, size_t) or
102 * StartEntryWithTime(const char*, size_t, time_t). This must be called before
103 * any new zip entries are started, or before Finish() is called.
104 * Returns 0 on success, and an error value < 0 on failure.
105 */
106 int32_t FinishEntry();
107
108 /**
109 * Writes the Central Directory Headers and flushes the zip file stream.
110 * Returns 0 on success, and an error value < 0 on failure.
111 */
112 int32_t Finish();
113
114private:
115 DISALLOW_COPY_AND_ASSIGN(ZipWriter);
116
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700117 struct FileInfo {
118 std::string path;
119 uint16_t compression_method;
120 uint32_t crc32;
121 uint32_t compressed_size;
122 uint32_t uncompressed_size;
123 uint16_t last_mod_time;
124 uint16_t last_mod_date;
125 uint32_t local_file_header_offset;
126 };
127
Adam Lesinski591fd392015-10-06 15:23:46 -0700128 int32_t HandleError(int32_t error_code);
129 int32_t PrepareDeflate();
130 int32_t StoreBytes(FileInfo* file, const void* data, size_t len);
131 int32_t CompressBytes(FileInfo* file, const void* data, size_t len);
132 int32_t FlushCompressedBytes(FileInfo* file);
133
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700134 enum class State {
135 kWritingZip,
136 kWritingEntry,
137 kDone,
138 kError,
139 };
140
141 FILE* file_;
142 off64_t current_offset_;
143 State state_;
144 std::vector<FileInfo> files_;
Adam Lesinski591fd392015-10-06 15:23:46 -0700145
146 std::unique_ptr<z_stream, void(*)(z_stream*)> z_stream_;
147 std::vector<uint8_t> buffer_;
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700148};
149
150#endif /* LIBZIPARCHIVE_ZIPWRITER_H_ */