| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef __LIBS_ZIPFILE_H |
| 22 |
#define __LIBS_ZIPFILE_H |
| 23 |
|
| 24 |
#include <utils/Vector.h> |
| 25 |
#include <utils/Errors.h> |
| 26 |
#include <stdio.h> |
| 27 |
|
| 28 |
#include "ZipEntry.h" |
| 29 |
|
| 30 |
namespace android { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class ZipFile { |
| 45 |
public: |
| 46 |
ZipFile(void) |
| 47 |
: mZipFp(NULL), mReadOnly(false), mNeedCDRewrite(false) |
| 48 |
{} |
| 49 |
~ZipFile(void) { |
| 50 |
if (!mReadOnly) |
| 51 |
flush(); |
| 52 |
if (mZipFp != NULL) |
| 53 |
fclose(mZipFp); |
| 54 |
discardEntries(); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
enum { |
| 61 |
kOpenReadOnly = 0x01, |
| 62 |
kOpenReadWrite = 0x02, |
| 63 |
kOpenCreate = 0x04, |
| 64 |
kOpenTruncate = 0x08, |
| 65 |
}; |
| 66 |
status_t open(const char* zipFileName, int flags); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
status_t add(const char* fileName, int compressionMethod, |
| 81 |
ZipEntry** ppEntry) |
| 82 |
{ |
| 83 |
return add(fileName, fileName, compressionMethod, ppEntry); |
| 84 |
} |
| 85 |
status_t add(const char* fileName, const char* storageName, |
| 86 |
int compressionMethod, ZipEntry** ppEntry) |
| 87 |
{ |
| 88 |
return addCommon(fileName, NULL, 0, storageName, |
| 89 |
ZipEntry::kCompressStored, |
| 90 |
compressionMethod, ppEntry); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
status_t addGzip(const char* fileName, const char* storageName, |
| 99 |
ZipEntry** ppEntry) |
| 100 |
{ |
| 101 |
return addCommon(fileName, NULL, 0, storageName, |
| 102 |
ZipEntry::kCompressDeflated, |
| 103 |
ZipEntry::kCompressDeflated, ppEntry); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
status_t add(const void* data, size_t size, const char* storageName, |
| 112 |
int compressionMethod, ZipEntry** ppEntry) |
| 113 |
{ |
| 114 |
return addCommon(NULL, data, size, storageName, |
| 115 |
ZipEntry::kCompressStored, |
| 116 |
compressionMethod, ppEntry); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
status_t add(const ZipFile* pSourceZip, const ZipEntry* pSourceEntry, |
| 127 |
int padding, ZipEntry** ppEntry); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
status_t remove(ZipEntry* pEntry); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
status_t flush(void); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
void* uncompress(const ZipEntry* pEntry); |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
ZipEntry* getEntryByName(const char* fileName) const; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
int getNumEntries(void) const { return mEntries.size(); } |
| 165 |
ZipEntry* getEntryByIndex(int idx) const; |
| 166 |
|
| 167 |
private: |
| 168 |
|
| 169 |
ZipFile(const ZipFile& src); |
| 170 |
ZipFile& operator=(const ZipFile& src); |
| 171 |
|
| 172 |
class EndOfCentralDir { |
| 173 |
public: |
| 174 |
EndOfCentralDir(void) : |
| 175 |
mDiskNumber(0), |
| 176 |
mDiskWithCentralDir(0), |
| 177 |
mNumEntries(0), |
| 178 |
mTotalNumEntries(0), |
| 179 |
mCentralDirSize(0), |
| 180 |
mCentralDirOffset(0), |
| 181 |
mCommentLen(0), |
| 182 |
mComment(NULL) |
| 183 |
{} |
| 184 |
virtual ~EndOfCentralDir(void) { |
| 185 |
delete[] mComment; |
| 186 |
} |
| 187 |
|
| 188 |
status_t readBuf(const unsigned char* buf, int len); |
| 189 |
status_t write(FILE* fp); |
| 190 |
|
| 191 |
|
| 192 |
unsigned short mDiskNumber; |
| 193 |
unsigned short mDiskWithCentralDir; |
| 194 |
unsigned short mNumEntries; |
| 195 |
unsigned short mTotalNumEntries; |
| 196 |
unsigned long mCentralDirSize; |
| 197 |
unsigned long mCentralDirOffset; |
| 198 |
unsigned short mCommentLen; |
| 199 |
unsigned char* mComment; |
| 200 |
|
| 201 |
enum { |
| 202 |
kSignature = 0x06054b50, |
| 203 |
kEOCDLen = 22, |
| 204 |
|
| 205 |
kMaxCommentLen = 65535, |
| 206 |
kMaxEOCDSearch = kMaxCommentLen + EndOfCentralDir::kEOCDLen, |
| 207 |
|
| 208 |
}; |
| 209 |
|
| 210 |
void dump(void) const; |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
status_t readCentralDir(void); |
| 216 |
|
| 217 |
|
| 218 |
status_t crunchArchive(void); |
| 219 |
|
| 220 |
|
| 221 |
void discardEntries(void); |
| 222 |
|
| 223 |
|
| 224 |
status_t addCommon(const char* fileName, const void* data, size_t size, |
| 225 |
const char* storageName, int sourceType, int compressionMethod, |
| 226 |
ZipEntry** ppEntry); |
| 227 |
|
| 228 |
|
| 229 |
status_t copyFpToFp(FILE* dstFp, FILE* srcFp, unsigned long* pCRC32); |
| 230 |
|
| 231 |
status_t copyDataToFp(FILE* dstFp, |
| 232 |
const void* data, size_t size, unsigned long* pCRC32); |
| 233 |
|
| 234 |
status_t copyPartialFpToFp(FILE* dstFp, FILE* srcFp, long length, |
| 235 |
unsigned long* pCRC32); |
| 236 |
|
| 237 |
status_t filemove(FILE* fp, off_t dest, off_t src, size_t n); |
| 238 |
|
| 239 |
status_t compressFpToFp(FILE* dstFp, FILE* srcFp, |
| 240 |
const void* data, size_t size, unsigned long* pCRC32); |
| 241 |
|
| 242 |
|
| 243 |
time_t getModTime(int fd); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
FILE* mZipFp; |
| 250 |
|
| 251 |
|
| 252 |
EndOfCentralDir mEOCD; |
| 253 |
|
| 254 |
|
| 255 |
bool mReadOnly; |
| 256 |
|
| 257 |
|
| 258 |
bool mNeedCDRewrite; |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
Vector<ZipEntry*> mEntries; |
| 266 |
}; |
| 267 |
|
| 268 |
}; |
| 269 |
|
| 270 |
#endif |