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.
19 #include "utils/ZipFile.h"
24 using namespace android;
31 fprintf(stderr, "Zip alignment utility\n");
33 "Usage: zipalign [-f] [-v] <align> infile.zip outfile.zip\n"
34 " zipalign -c [-v] <align> infile.zip\n" );
38 * Copy all entries from "pZin" to "pZout", aligning as needed.
40 static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment)
42 int numEntries = pZin->getNumEntries();
47 for (int i = 0; i < numEntries; i++) {
51 pEntry = pZin->getEntryByIndex(i);
53 fprintf(stderr, "ERROR: unable to retrieve entry %d\n", i);
57 if (pEntry->isCompressed()) {
58 /* copy the entry without padding */
59 //printf("--- %s: orig at %ld len=%ld (compressed)\n",
60 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
61 // (long) pEntry->getUncompressedLen());
65 * Copy the entry, adjusting as required. We assume that the
66 * file position in the new file will be equal to the file
67 * position in the original.
69 long newOffset = pEntry->getFileOffset() + bias;
70 padding = (alignment - (newOffset % alignment)) % alignment;
72 //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
73 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
74 // bias, (long) pEntry->getUncompressedLen(), padding);
77 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
78 if (status != NO_ERROR)
81 //printf(" added '%s' at %ld (pad=%d)\n",
82 // pNewEntry->getFileName(), (long) pNewEntry->getFileOffset(),
90 * Process a file. We open the input and output files, failing if the
91 * output file exists and "force" wasn't specified.
93 static int process(const char* inFileName, const char* outFileName,
94 int alignment, bool force)
98 //printf("PROCESS: align=%d in='%s' out='%s' force=%d\n",
99 // alignment, inFileName, outFileName, force);
101 /* this mode isn't supported -- do a trivial check */
102 if (strcmp(inFileName, outFileName) == 0) {
103 fprintf(stderr, "Input and output can't be same file\n");
107 /* don't overwrite existing unless given permission */
108 if (!force && access(outFileName, F_OK) == 0) {
109 fprintf(stderr, "Output file '%s' exists\n", outFileName);
113 if (zin.open(inFileName, ZipFile::kOpenReadOnly) != NO_ERROR) {
114 fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName);
117 if (zout.open(outFileName,
118 ZipFile::kOpenReadWrite|ZipFile::kOpenCreate|ZipFile::kOpenTruncate)
121 fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName);
125 int result = copyAndAlign(&zin, &zout, alignment);
127 printf("zipalign: failed rewriting '%s' to '%s'\n",
128 inFileName, outFileName);
134 * Verify the alignment of a zip archive.
136 static int verify(const char* fileName, int alignment, bool verbose)
139 bool foundBad = false;
142 printf("Verifying alignment of %s (%d)...\n", fileName, alignment);
144 if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != NO_ERROR) {
145 fprintf(stderr, "Unable to open '%s' for verification\n", fileName);
149 int numEntries = zipFile.getNumEntries();
152 for (int i = 0; i < numEntries; i++) {
153 pEntry = zipFile.getEntryByIndex(i);
154 if (pEntry->isCompressed()) {
156 printf("%8ld %s (OK - compressed)\n",
157 (long) pEntry->getFileOffset(), pEntry->getFileName());
160 long offset = pEntry->getFileOffset();
161 if ((offset % alignment) != 0) {
163 printf("%8ld %s (BAD - %ld)\n",
164 (long) offset, pEntry->getFileName(),
170 printf("%8ld %s (OK)\n",
171 (long) offset, pEntry->getFileName());
178 printf("Verification %s\n", foundBad ? "FAILED" : "succesful");
180 return foundBad ? 1 : 0;
186 int main(int argc, char* const argv[])
188 bool wantUsage = false;
191 bool verbose = false;
204 while (argc && argv[0][0] == '-') {
205 const char* cp = argv[0] +1;
207 while (*cp != '\0') {
219 fprintf(stderr, "ERROR: unknown flag -%c\n", *cp);
231 if (!((check && argc == 2) || (!check && argc == 3))) {
236 alignment = strtol(argv[0], &endp, 10);
237 if (*endp != '\0' || alignment <= 0) {
238 fprintf(stderr, "Invalid value for alignment: %s\n", argv[0]);
244 /* check existing archive for correct alignment */
245 result = verify(argv[1], alignment, verbose);
247 /* create the new archive */
248 result = process(argv[1], argv[2], alignment, force);
250 /* trust, but verify */
252 result = verify(argv[2], alignment, verbose);