Add Zopfli-recompress option to zipalign

Zopfli provides compression roughly 5% better than zlib, while remaining
completely compatible with zlib decoders. This patch adds a "-z" option
to zipalign, recompressing all compressed files within the zip archive.

Change-Id: If177ca4b82ec701b7446861b2cfe08c6bd403813
This commit is contained in:
Raph Levien
2014-07-07 16:00:29 -07:00
parent 5769e2a0c0
commit 093d04c631
4 changed files with 207 additions and 108 deletions

View File

@@ -32,19 +32,20 @@ void usage(void)
fprintf(stderr, "Zip alignment utility\n");
fprintf(stderr, "Copyright (C) 2009 The Android Open Source Project\n\n");
fprintf(stderr,
"Usage: zipalign [-f] [-v] <align> infile.zip outfile.zip\n"
"Usage: zipalign [-f] [-v] [-z] <align> infile.zip outfile.zip\n"
" zipalign -c [-v] <align> infile.zip\n\n" );
fprintf(stderr,
" <align>: alignment in bytes, e.g. '4' provides 32-bit alignment\n");
fprintf(stderr, " -c: check alignment only (does not modify file)\n");
fprintf(stderr, " -f: overwrite existing outfile.zip\n");
fprintf(stderr, " -v: verbose output\n");
fprintf(stderr, " -z: recompress using Zopfli\n");
}
/*
* Copy all entries from "pZin" to "pZout", aligning as needed.
*/
static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment)
static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli)
{
int numEntries = pZin->getNumEntries();
ZipEntry* pEntry;
@@ -67,6 +68,12 @@ static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment)
// pEntry->getFileName(), (long) pEntry->getFileOffset(),
// (long) pEntry->getUncompressedLen());
if (zopfli) {
status = pZout->addRecompress(pZin, pEntry, &pNewEntry);
bias += pNewEntry->getCompressedLen() - pEntry->getCompressedLen();
} else {
status = pZout->add(pZin, pEntry, padding, &pNewEntry);
}
} else {
/*
* Copy the entry, adjusting as required. We assume that the
@@ -79,9 +86,9 @@ static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment)
//printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
// pEntry->getFileName(), (long) pEntry->getFileOffset(),
// bias, (long) pEntry->getUncompressedLen(), padding);
status = pZout->add(pZin, pEntry, padding, &pNewEntry);
}
status = pZout->add(pZin, pEntry, padding, &pNewEntry);
if (status != NO_ERROR)
return 1;
bias += padding;
@@ -98,7 +105,7 @@ static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment)
* output file exists and "force" wasn't specified.
*/
static int process(const char* inFileName, const char* outFileName,
int alignment, bool force)
int alignment, bool force, bool zopfli)
{
ZipFile zin, zout;
@@ -129,7 +136,7 @@ static int process(const char* inFileName, const char* outFileName,
return 1;
}
int result = copyAndAlign(&zin, &zout, alignment);
int result = copyAndAlign(&zin, &zout, alignment, zopfli);
if (result != 0) {
printf("zipalign: failed rewriting '%s' to '%s'\n",
inFileName, outFileName);
@@ -196,6 +203,7 @@ int main(int argc, char* const argv[])
bool check = false;
bool force = false;
bool verbose = false;
bool zopfli = false;
int result = 1;
int alignment;
char* endp;
@@ -222,6 +230,9 @@ int main(int argc, char* const argv[])
case 'v':
verbose = true;
break;
case 'z':
zopfli = true;
break;
default:
fprintf(stderr, "ERROR: unknown flag -%c\n", *cp);
wantUsage = true;
@@ -252,7 +263,7 @@ int main(int argc, char* const argv[])
result = verify(argv[1], alignment, verbose);
} else {
/* create the new archive */
result = process(argv[1], argv[2], alignment, force);
result = process(argv[1], argv[2], alignment, force, zopfli);
/* trust, but verify */
if (result == 0)