File: cookbook.md

package info (click to toggle)
zipflinger 7.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,076 kB
  • sloc: java: 4,491; xml: 69; makefile: 12
file content (124 lines) | stat: -rw-r--r-- 3,574 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Zipflinger cheatsheet

Zipflinger is a library dedicated to ZIP files manipulation.  It is capable of adding and removing
entries without decompressing/compressing the whole archive. It supports:

- Listing content of a zip archive.
- Deleting entry in an archive.
- Adding entries in an archive with source from filesystem, memory, and other zip archives.

Deleting a non-existing entry will fail silently.
Adding an existing entry will not silently overwrite but will throw an exception instead.

## How to list the content of an archive
```
Map<String, Entry> map = ZipArchive.listEntries(new File("/path/to/zip"));
for(Entry entry : map.getEntries().values()) {
    entry.getName();
    entry.getCrc();
    ...
}
```

## How to replace an entry in an archive
```
 ZipArchive zip = new ZipArchive("app.apk");
 zip.delete("classes18.dex"); // All deletes must be submitted first.
 zip.add(new BytesSource(new File("classes18.dex"), "classes18.dex", Deflater.BEST_SPEED));
 zip.add(new BytesSource(new File("img.png"), "image.png", Deflater.NO_COMPRESSION));
 zip.close();
```

## How to merge two zips into one
```
 ZipArchive zip = new ZipArchive("app.apk");

 ZipSource zipSource1 = ZipSource.selectAll(new File("/path/to/zip1.zip"));
 zip.add(zipSource1);

 ZipSource zipSource2 = ZipSource.selectAll(new File("/path/to/zip2.zip"));
 zip.add(zipSource2);

 zip.close();
```

## How to copy a zip entry from an other zip into an existing apk
```
 ZipArchive zip = new ZipArchive("app.apk");

 ZipSource zipSource = new ZipSource(new File("/path/to/zip1.zip"));

 zipSource.select("classes18.dex", "classes18NewName.dex"); // non-aligned (default)

 ZipSourceEntry alignedEntry = zipSource.select("lib.so", "lib.so"); // aligned
 alignedEntry.align(4);

 zip.addZipSource(zipSource);

 zip.close();
```

## How to iterate over a zip source entries and select only a few
```
 ZipArchive zip = new ZipArchive("app.apk");
 ZipSource zipSource = new ZipSource(new File("/path/to/zip1.zip"));
 for(String name : zipSource.entries().keys()) {
     if (youwantIt) {
         zipSource.select(name, "newName");
     }
 }
 zip.add(zipSource);
 zip.close();
```

## Generate multiple zips from one zip source
Creating a ZipSource is not an I/O free operation since the CD of the source archive has to be parsed.
In the case where one source zip is to be used to generate multiple destination zips, parsing
can be done only once by providing the same ZipMap to each ZipSource.

```
 // The source zip is parsed only once.
 ZipMap map = ZipMap.from(new File("source.zip"));

 ZipSource zipSource1 = new ZipSource(map);
 zipSource1.select("a", "a");
 try(ZipArchive archive = new ZipArchive("dest1.zip")) {
   archive.add(zipSource1);
 }


 ZipSource zipSource2 = new ZipSource(map);
 zipSource2.select("b", "b");
 try(ZipArchive archive = new ZipArchive("dest2.zip")) {
   archive.add(zipSource2);
 }

```

# Add files to a zip and preserve executable permission
```
try(ZipArchive zip = new ZipArchive("archive.zip")) {
  String p = "/path/x";
  int c = Deflater.NO_COMPRESSION;
  zip.add(new FullFileSource(p, "x", c));
}
```

# Add symbolic links to a zip
```
try(ZipArchive zip = new ZipArchive("archive.zip")) {
  String p = "/path/x";
  int c = Deflater.NO_COMPRESSION;
  FullFileSource.Symlink perm = FullFileSource.Symlink.DO_NOT_FOLLOW;
  zip.add(new FullFileSource(p  , "x", c, perm));
}
```

# How to extract content from an archive
```
 try(ZipRepo repo = new ZipRepo("source.zip")) {
   try(InputStream inputStream = repo.getContent("entryName")) {
   ...
   }
 }
```