write signature block with alignment

This commit is contained in:
REAndroid 2023-04-26 15:52:17 +02:00
parent fb7e41c638
commit f0c2ebb97d
3 changed files with 40 additions and 11 deletions

View File

@ -28,6 +28,10 @@ public class ZipFileInput extends ZipInput {
this.file = file;
}
public File getFile(){
return file;
}
@Override
public long position() throws IOException {
FileChannel fileChannel = this.fileChannel;

View File

@ -26,11 +26,13 @@ import com.reandroid.archive2.io.ZipFileOutput;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class ApkWriter extends ZipFileOutput {
private final Object mLock = new Object();
private final Collection<? extends InputSource> sourceList;
private ZipAligner zipAligner;
private ApkSignatureBlock apkSignatureBlock;
@ -42,6 +44,7 @@ public class ApkWriter extends ZipFileOutput {
this.zipAligner = ZipAligner.apkAligner();
}
public void write()throws IOException {
synchronized (mLock){
List<OutputSource> outputList = buildOutputEntry();
logMessage("Buffering compress changed files ...");
BufferFileInput buffer = writeBuffer(outputList);
@ -54,6 +57,8 @@ public class ApkWriter extends ZipFileOutput {
writeCEH(outputList);
this.close();
logMessage("Written to: " + getFile().getName());
}
}
public void setApkSignatureBlock(ApkSignatureBlock apkSignatureBlock) {
this.apkSignatureBlock = apkSignatureBlock;
@ -92,7 +97,16 @@ public class ApkWriter extends ZipFileOutput {
return;
}
logMessage("Writing signature block ...");
signatureBlock.writeBytes(getOutputStream());
long offset = position();
int alignment = 4096;
int padding = (int) ((alignment - (offset % alignment)) % alignment);
OutputStream outputStream = getOutputStream();
if(padding > 0){
outputStream.write(new byte[padding]);
}
signatureBlock.refresh();
logVerbose("padding = " + padding + ", signatures = " + signatureBlock.countBytes());
signatureBlock.writeBytes(outputStream);
}
private BufferFileInput writeBuffer(List<OutputSource> outputList) throws IOException {
File bufferFile = getBufferFile();

View File

@ -38,4 +38,15 @@ public class BufferFileInput extends ZipFileInput {
}
throw new IOException("File locked!");
}
@Override
public void close() throws IOException {
super.close();
if(unlocked){
File file = super.getFile();
if(file.isFile()){
file.delete();
}
unlocked = false;
}
}
}