2 * Copyright (C) 2008 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.
25 using namespace android;
32 fprintf(stderr, "Zip alignment utility\n");
33 fprintf(stderr, "Copyright (C) 2009 The Android Open Source Project\n\n");
35 "Usage: zipalign [-f] [-v] [-z] <align> infile.zip outfile.zip\n"
36 " zipalign -c [-v] <align> infile.zip\n\n" );
38 " <align>: alignment in bytes, e.g. '4' provides 32-bit alignment\n");
39 fprintf(stderr, " -c: check alignment only (does not modify file)\n");
40 fprintf(stderr, " -f: overwrite existing outfile.zip\n");
41 fprintf(stderr, " -v: verbose output\n");
42 fprintf(stderr, " -z: recompress using Zopfli\n");
46 * Copy all entries from "pZin" to "pZout", aligning as needed.
48 static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli)
50 int numEntries = pZin->getNumEntries();
55 for (int i = 0; i < numEntries; i++) {
59 pEntry = pZin->getEntryByIndex(i);
61 fprintf(stderr, "ERROR: unable to retrieve entry %d\n", i);
65 if (pEntry->isCompressed()) {
66 /* copy the entry without padding */
67 //printf("--- %s: orig at %ld len=%ld (compressed)\n",
68 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
69 // (long) pEntry->getUncompressedLen());
72 status = pZout->addRecompress(pZin, pEntry, &pNewEntry);
73 bias += pNewEntry->getCompressedLen() - pEntry->getCompressedLen();
75 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
79 * Copy the entry, adjusting as required. We assume that the
80 * file position in the new file will be equal to the file
81 * position in the original.
83 long newOffset = pEntry->getFileOffset() + bias;
84 padding = (alignment - (newOffset % alignment)) % alignment;
86 //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
87 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
88 // bias, (long) pEntry->getUncompressedLen(), padding);
89 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
92 if (status != NO_ERROR)
95 //printf(" added '%s' at %ld (pad=%d)\n",
96 // pNewEntry->getFileName(), (long) pNewEntry->getFileOffset(),
104 * Process a file. We open the input and output files, failing if the
105 * output file exists and "force" wasn't specified.
107 static int process(const char* inFileName, const char* outFileName,
108 int alignment, bool force, bool zopfli)
112 //printf("PROCESS: align=%d in='%s' out='%s' force=%d\n",
113 // alignment, inFileName, outFileName, force);
115 /* this mode isn't supported -- do a trivial check */
116 if (strcmp(inFileName, outFileName) == 0) {
117 fprintf(stderr, "Input and output can't be same file\n");
121 /* don't overwrite existing unless given permission */
122 if (!force && access(outFileName, F_OK) == 0) {
123 fprintf(stderr, "Output file '%s' exists\n", outFileName);
127 if (zin.open(inFileName, ZipFile::kOpenReadOnly) != NO_ERROR) {
128 fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName);
131 if (zout.open(outFileName,
132 ZipFile::kOpenReadWrite|ZipFile::kOpenCreate|ZipFile::kOpenTruncate)
135 fprintf(stderr, "Unable to open '%s' as zip archive\n", outFileName);
139 int result = copyAndAlign(&zin, &zout, alignment, zopfli);
141 printf("zipalign: failed rewriting '%s' to '%s'\n",
142 inFileName, outFileName);
148 * Verify the alignment of a zip archive.
150 static int verify(const char* fileName, int alignment, bool verbose)
153 bool foundBad = false;
156 printf("Verifying alignment of %s (%d)...\n", fileName, alignment);
158 if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != NO_ERROR) {
159 fprintf(stderr, "Unable to open '%s' for verification\n", fileName);
163 int numEntries = zipFile.getNumEntries();
166 for (int i = 0; i < numEntries; i++) {
167 pEntry = zipFile.getEntryByIndex(i);
168 if (pEntry->isCompressed()) {
170 printf("%8ld %s (OK - compressed)\n",
171 (long) pEntry->getFileOffset(), pEntry->getFileName());
174 long offset = pEntry->getFileOffset();
175 if ((offset % alignment) != 0) {
177 printf("%8ld %s (BAD - %ld)\n",
178 (long) offset, pEntry->getFileName(),
184 printf("%8ld %s (OK)\n",
185 (long) offset, pEntry->getFileName());
192 printf("Verification %s\n", foundBad ? "FAILED" : "succesful");
194 return foundBad ? 1 : 0;
200 int main(int argc, char* const argv[])
202 bool wantUsage = false;
205 bool verbose = false;
219 while (argc && argv[0][0] == '-') {
220 const char* cp = argv[0] +1;
222 while (*cp != '\0') {
237 fprintf(stderr, "ERROR: unknown flag -%c\n", *cp);
249 if (!((check && argc == 2) || (!check && argc == 3))) {
254 alignment = strtol(argv[0], &endp, 10);
255 if (*endp != '\0' || alignment <= 0) {
256 fprintf(stderr, "Invalid value for alignment: %s\n", argv[0]);
262 /* check existing archive for correct alignment */
263 result = verify(argv[1], alignment, verbose);
265 /* create the new archive */
266 result = process(argv[1], argv[2], alignment, force, zopfli);
268 /* trust, but verify */
270 result = verify(argv[2], alignment, verbose);