Skip to content

Commit 18e52b1

Browse files
committed
[tinker] Make resources.apk 4 bytes aligned to save memory cost.
1 parent 19a7713 commit 18e52b1

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

third-party/tinker-ziputils/src/main/java/com/tencent/tinker/ziputils/ziputil/TinkerZipOutputStream.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.OutputStream;
2626
import java.util.HashSet;
2727
import java.util.zip.GZIPOutputStream;
28+
import java.util.zip.ZipEntry;
2829
import java.util.zip.ZipException;
2930
import java.util.zip.ZipInputStream;
3031

@@ -117,6 +118,11 @@ public class TinkerZipOutputStream extends FilterOutputStream implements ZipCons
117118
* to the start of the current entry header is greater than 0xFFFFFFFF.
118119
*/
119120
private boolean currentEntryNeedsZip64;
121+
122+
private final int alignBytes;
123+
124+
private int padding = 0;
125+
120126
/**
121127
* Constructs a new {@code ZipOutputStream} that writes a zip file to the given
122128
* {@code OutputStream}.
@@ -130,8 +136,13 @@ public TinkerZipOutputStream(OutputStream os) {
130136
* @hide for testing only.
131137
*/
132138
public TinkerZipOutputStream(OutputStream os, boolean forceZip64) {
139+
this(os, forceZip64, 4);
140+
}
141+
142+
public TinkerZipOutputStream(OutputStream os, boolean forceZip64, int alignBytes) {
133143
super(os);
134144
this.forceZip64 = forceZip64;
145+
this.alignBytes = alignBytes;
135146
}
136147

137148
/**
@@ -328,7 +339,8 @@ public void closeEntry() throws IOException {
328339
if (currentEntry.extra != null) {
329340
cDir.write(currentEntry.extra);
330341
}
331-
offset += curOffset;
342+
offset += curOffset + padding;
343+
padding = 0;
332344
if (entryCommentBytes.length > 0) {
333345
cDir.write(entryCommentBytes);
334346
entryCommentBytes = BYTE;
@@ -390,7 +402,7 @@ public void finish() throws IOException {
390402
writeIntAsUint16(cDir, entries.size()); // Number of entries
391403
writeIntAsUint16(cDir, entries.size()); // Number of entries
392404
writeLongAsUint32(cDir, cdirEntriesSize); // Size of central dir
393-
writeLongAsUint32(cDir, offset); // Offset of central dir
405+
writeLongAsUint32(cDir, offset + padding); // Offset of central dir
394406
}
395407
writeIntAsUint16(cDir, commentBytes.length);
396408
if (commentBytes.length > 0) {
@@ -401,6 +413,22 @@ public void finish() throws IOException {
401413
cDir = null;
402414
}
403415

416+
private int getPaddingByteCount(TinkerZipEntry entry, long entryFileOffset) {
417+
if (entry.getMethod() != ZipEntry.STORED || alignBytes == 0) {
418+
return 0;
419+
}
420+
return (int) ((alignBytes - (entryFileOffset % alignBytes)) % alignBytes);
421+
}
422+
423+
private void makePaddingToStream(OutputStream os, long padding) throws IOException {
424+
if (padding <= 0) {
425+
return;
426+
}
427+
while (padding-- > 0) {
428+
os.write(0);
429+
}
430+
}
431+
404432
/**
405433
* Writes entry information to the underlying stream. Data associated with
406434
* the entry can then be written using {@code write()}. After data is
@@ -502,19 +530,23 @@ public void putNextEntry(TinkerZipEntry ze) throws IOException {
502530
writeLongAsUint32(out, 0);
503531
writeLongAsUint32(out, 0);
504532
}
505-
writeIntAsUint16(out, nameBytes.length);
533+
final int nameLength = nameBytes.length;
534+
writeIntAsUint16(out, nameLength);
535+
final long currDataOffset = offset + LOCHDR + nameLength + (currentEntry.getExtra() != null ? currentEntry.getExtra().length : 0);
536+
padding = getPaddingByteCount(currentEntry, currDataOffset);
506537
/*if (currentEntryNeedsZip64) {
507538
Zip64.insertZip64ExtendedInfoToExtras(currentEntry);
508539
}*/
509540
if (currentEntry.extra != null) {
510-
writeIntAsUint16(out, currentEntry.extra.length);
541+
writeIntAsUint16(out, currentEntry.extra.length + padding);
511542
} else {
512-
writeIntAsUint16(out, 0);
543+
writeIntAsUint16(out, padding);
513544
}
514545
out.write(nameBytes);
515546
if (currentEntry.extra != null) {
516547
out.write(currentEntry.extra);
517548
}
549+
makePaddingToStream(out, padding);
518550
}
519551

520552
/**

0 commit comments

Comments
 (0)