2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 // Read-only access to Zip archives, with minimal heap allocation.
20 #define LOG_TAG "zipro"
21 //#define LOG_NDEBUG 0
22 #include <androidfw/ZipFileRO.h>
23 #include <utils/Log.h>
24 #include <utils/Compat.h>
25 #include <utils/misc.h>
26 #include <utils/threads.h>
27 #include <ziparchive/zip_archive.h>
37 using namespace android;
45 _ZipEntryRO() : cookie(NULL) {}
52 _ZipEntryRO(const _ZipEntryRO& other);
53 _ZipEntryRO& operator=(const _ZipEntryRO& other);
56 ZipFileRO::~ZipFileRO() {
57 CloseArchive(mHandle);
62 * Open the specified file read-only. We memory-map the entire thing and
63 * close the file before returning.
65 /* static */ ZipFileRO* ZipFileRO::open(const char* zipFileName)
67 ZipArchiveHandle handle;
68 const int32_t error = OpenArchive(zipFileName, &handle);
70 ALOGW("Error opening archive %s: %s", zipFileName, ErrorCodeString(error));
75 return new ZipFileRO(handle, strdup(zipFileName));
79 ZipEntryRO ZipFileRO::findEntryByName(const char* entryName) const
81 _ZipEntryRO* data = new _ZipEntryRO;
83 data->name = ZipString(entryName);
85 const int32_t error = FindEntry(mHandle, data->name, &(data->entry));
91 return (ZipEntryRO) data;
95 * Get the useful fields from the zip entry.
97 * Returns "false" if the offsets to the fields or the contents of the fields
100 bool ZipFileRO::getEntryInfo(ZipEntryRO entry, uint16_t* pMethod,
101 uint32_t* pUncompLen, uint32_t* pCompLen, off64_t* pOffset,
102 uint32_t* pModWhen, uint32_t* pCrc32) const
104 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
105 const ZipEntry& ze = zipEntry->entry;
107 if (pMethod != NULL) {
108 *pMethod = ze.method;
110 if (pUncompLen != NULL) {
111 *pUncompLen = ze.uncompressed_length;
113 if (pCompLen != NULL) {
114 *pCompLen = ze.compressed_length;
116 if (pOffset != NULL) {
117 *pOffset = ze.offset;
119 if (pModWhen != NULL) {
120 *pModWhen = ze.mod_time;
122 if (pCrc32 != NULL) {
129 bool ZipFileRO::startIteration(void** cookie) {
130 return startIteration(cookie, NULL, NULL);
133 bool ZipFileRO::startIteration(void** cookie, const char* prefix, const char* suffix)
135 _ZipEntryRO* ze = new _ZipEntryRO;
136 ZipString pe(prefix ? prefix : "");
137 ZipString se(suffix ? suffix : "");
138 int32_t error = StartIteration(mHandle, &(ze->cookie),
140 suffix ? &se : NULL);
142 ALOGW("Could not start iteration over %s: %s", mFileName, ErrorCodeString(error));
151 ZipEntryRO ZipFileRO::nextEntry(void* cookie)
153 _ZipEntryRO* ze = reinterpret_cast<_ZipEntryRO*>(cookie);
154 int32_t error = Next(ze->cookie, &(ze->entry), &(ze->name));
157 ALOGW("Error iteration over %s: %s", mFileName, ErrorCodeString(error));
165 void ZipFileRO::endIteration(void* cookie)
167 delete reinterpret_cast<_ZipEntryRO*>(cookie);
170 void ZipFileRO::releaseEntry(ZipEntryRO entry) const
172 delete reinterpret_cast<_ZipEntryRO*>(entry);
176 * Copy the entry's filename to the buffer.
178 int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, size_t bufLen)
181 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
182 const uint16_t requiredSize = zipEntry->name.name_length + 1;
184 if (bufLen < requiredSize) {
185 ALOGW("Buffer too short, requires %d bytes for entry name", requiredSize);
189 memcpy(buffer, zipEntry->name.name, requiredSize - 1);
190 buffer[requiredSize - 1] = '\0';
196 * Create a new FileMap object that spans the data in "entry".
198 FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
200 const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
201 const ZipEntry& ze = zipEntry->entry;
202 int fd = GetFileDescriptor(mHandle);
203 size_t actualLen = 0;
205 if (ze.method == kCompressStored) {
206 actualLen = ze.uncompressed_length;
208 actualLen = ze.compressed_length;
211 FileMap* newMap = new FileMap();
212 if (!newMap->create(mFileName, fd, ze.offset, actualLen, true)) {
221 * Uncompress an entry, in its entirety, into the provided output buffer.
223 * This doesn't verify the data's CRC, which might be useful for
224 * uncompressed data. The caller should be able to manage it.
226 bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer, size_t size) const
228 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
229 const int32_t error = ExtractToMemory(mHandle, &(zipEntry->entry),
230 (uint8_t*) buffer, size);
232 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
240 * Uncompress an entry, in its entirety, to an open file descriptor.
242 * This doesn't verify the data's CRC, but probably should.
244 bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
246 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
247 const int32_t error = ExtractEntryToFile(mHandle, &(zipEntry->entry), fd);
249 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));