File size: 2,772 Bytes
13ca229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.*;
import java.nio.file.*;
import java.util.zip.*;

/**
 * Repack an APK so that shared libraries are STORED (uncompressed).
 *
 * Why: System.loadLibrary resolves lib/<abi>/x.so directly inside the APK
 * (base.apk!/lib/<abi>/...). That requires the entry to be STORED and page
 * aligned, otherwise the loader reports
 *   UnsatisfiedLinkError: couldn't find "libx.so"
 *
 * .NET's ZipArchive CompressionLevel.NoCompression does not reliably produce
 * STORED entries from PowerShell, so this tiny tool does it explicitly.
 *
 * Usage: java ZipStore in.apk out.apk [prefix ...]
 *   default prefixes: lib/
 */
public final class ZipStore {

    public static void main(String[] args) throws Exception {
        if (args.length < 2) {
            System.err.println("usage: ZipStore <in.apk> <out.apk> [storedPrefix...]");
            System.exit(2);
        }
        Path in = Path.of(args[0]);
        Path out = Path.of(args[1]);
        String[] prefixes = args.length > 2 ? java.util.Arrays.copyOfRange(args, 2, args.length)
                : new String[]{"lib/"};

        try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(Files.newInputStream(in)));
             ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(out)))) {
            ZipEntry e;
            byte[] buf = new byte[1 << 16];
            while ((e = zis.getNextEntry()) != null) {
                if (e.isDirectory()) {
                    continue;
                }
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                int n;
                while ((n = zis.read(buf)) > 0) {
                    bos.write(buf, 0, n);
                }
                byte[] data = bos.toByteArray();

                ZipEntry ne = new ZipEntry(e.getName());
                ne.setTime(0L); // reproducible
                boolean store = false;
                for (String p : prefixes) {
                    if (e.getName().startsWith(p)) {
                        store = true;
                        break;
                    }
                }
                if (store) {
                    CRC32 crc = new CRC32();
                    crc.update(data);
                    ne.setMethod(ZipEntry.STORED);
                    ne.setSize(data.length);
                    ne.setCompressedSize(data.length);
                    ne.setCrc(crc.getValue());
                } else {
                    ne.setMethod(ZipEntry.DEFLATED);
                }
                zos.putNextEntry(ne);
                zos.write(data);
                zos.closeEntry();
                System.out.println((store ? "STORED   " : "deflated ") + e.getName() + "  " + data.length);
            }
        }
    }
}