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] <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");
45 * Copy all entries from "pZin" to "pZout", aligning as needed.
47 static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment)
49 int numEntries = pZin->getNumEntries();
54 for (int i = 0; i < numEntries; i++) {
58 pEntry = pZin->getEntryByIndex(i);
60 fprintf(stderr, "ERROR: unable to retrieve entry %d\n", i);
64 if (pEntry->isCompressed()) {
65 /* copy the entry without padding */
66 //printf("--- %s: orig at %ld len=%ld (compressed)\n",
67 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
68 // (long) pEntry->getUncompressedLen());
72 * Copy the entry, adjusting as required. We assume that the
73 * file position in the new file will be equal to the file
74 * position in the original.
76 long newOffset = pEntry->getFileOffset() + bias;
77 padding = (alignment - (newOffset % alignment)) % alignment;
79 //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
80 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
81 // bias, (long) pEntry->getUncompressedLen(), padding);
84 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
85 if (status != NO_ERROR)
88 //printf(" added '%s' at %ld (pad=%d)\n",
89 // pNewEntry->getFileName(), (long) pNewEntry->getFileOffset(),
97 * Process a file. We open the input and output files, failing if the
98 * output file exists and "force" wasn't specified.
100 static int process(const char* inFileName, const char* outFileName,
101 int alignment, bool force)
105 //printf("PROCESS: align=%d in='%s' out='%s' force=%d\n",
106 // alignment, inFileName, outFileName, force);
108 /* this mode isn't supported -- do a trivial check */
109 if (strcmp(inFileName, outFileName) == 0) {
110 fprintf(stderr, "Input and output can't be same file\n");
114 /* don't overwrite existing unless given permission */
115 if (!force && access(outFileName, F_OK) == 0) {
116 fprintf(stderr, "Output file '%s' exists\n", outFileName);
120 if (zin.open(inFileName, ZipFile::kOpenReadOnly) != NO_ERROR) {
121 fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName);
124 if (zout.open(outFileName,
125 ZipFile::kOpenReadWrite|ZipFile::kOpenCreate|ZipFile::kOpenTruncate)
128 fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName);
132 int result = copyAndAlign(&zin, &zout, alignment);
134 printf("zipalign: failed rewriting '%s' to '%s'\n",
135 inFileName, outFileName);
141 * Verify the alignment of a zip archive.
143 static int verify(const char* fileName, int alignment, bool verbose)
146 bool foundBad = false;
149 printf("Verifying alignment of %s (%d)...\n", fileName, alignment);
151 if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != NO_ERROR) {
152 fprintf(stderr, "Unable to open '%s' for verification\n", fileName);
156 int numEntries = zipFile.getNumEntries();
159 for (int i = 0; i < numEntries; i++) {
160 pEntry = zipFile.getEntryByIndex(i);
161 if (pEntry->isCompressed()) {
163 printf("%8ld %s (OK - compressed)\n",
164 (long) pEntry->getFileOffset(), pEntry->getFileName());
167 long offset = pEntry->getFileOffset();
168 if ((offset % alignment) != 0) {
170 printf("%8ld %s (BAD - %ld)\n",
171 (long) offset, pEntry->getFileName(),
177 printf("%8ld %s (OK)\n",
178 (long) offset, pEntry->getFileName());
185 printf("Verification %s\n", foundBad ? "FAILED" : "succesful");
187 return foundBad ? 1 : 0;
193 int main(int argc, char* const argv[])
195 bool wantUsage = false;
198 bool verbose = false;
211 while (argc && argv[0][0] == '-') {
212 const char* cp = argv[0] +1;
214 while (*cp != '\0') {
226 fprintf(stderr, "ERROR: unknown flag -%c\n", *cp);
238 if (!((check && argc == 2) || (!check && argc == 3))) {
243 alignment = strtol(argv[0], &endp, 10);
244 if (*endp != '\0' || alignment <= 0) {
245 fprintf(stderr, "Invalid value for alignment: %s\n", argv[0]);
251 /* check existing archive for correct alignment */
252 result = verify(argv[1], alignment, verbose);
254 /* create the new archive */
255 result = process(argv[1], argv[2], alignment, force);
257 /* trust, but verify */
259 result = verify(argv[2], alignment, verbose);