diff --git a/app/src/main/java/app/revanced/manager/patcher/Aligning.kt b/app/src/main/java/app/revanced/manager/patcher/Aligning.kt index 56d4b70..d8a672d 100644 --- a/app/src/main/java/app/revanced/manager/patcher/Aligning.kt +++ b/app/src/main/java/app/revanced/manager/patcher/Aligning.kt @@ -30,7 +30,7 @@ object Aligning { } file.copyEntriesFromFileAligned( - ZipFile(inputFile), + ZipFile(inputFile, readonly = true), ZipAligner::getEntryAlignment ) } diff --git a/app/src/main/java/app/revanced/manager/patcher/alignment/zip/ZipFile.kt b/app/src/main/java/app/revanced/manager/patcher/alignment/zip/ZipFile.kt index faacdaf..a49ffed 100644 --- a/app/src/main/java/app/revanced/manager/patcher/alignment/zip/ZipFile.kt +++ b/app/src/main/java/app/revanced/manager/patcher/alignment/zip/ZipFile.kt @@ -5,16 +5,17 @@ import app.revanced.manager.patcher.alignment.zip.structures.ZipEntry import java.io.Closeable import java.io.File +import java.io.IOException import java.io.RandomAccessFile import java.nio.ByteBuffer import java.nio.channels.FileChannel import java.util.zip.CRC32 import java.util.zip.Deflater -class ZipFile(file: File) : Closeable { +class ZipFile(file: File, private val readonly: Boolean = false) : Closeable { var entries: MutableList = mutableListOf() - private val filePointer: RandomAccessFile = RandomAccessFile(file, "rw") + private val filePointer: RandomAccessFile = RandomAccessFile(file, if (readonly) "r" else "rw") private var CDNeedsRewrite = false private val compressionLevel = 5 @@ -34,6 +35,10 @@ class ZipFile(file: File) : Closeable { filePointer.seek(0) } + private fun assertWritable() { + if (readonly) throw IOException("Archive is read-only") + } + private fun findEndRecord(): ZipEndRecord { //look from end to start since end record is at the end for (i in filePointer.length() - 1 downTo 0) { @@ -110,6 +115,8 @@ class ZipFile(file: File) : Closeable { } fun addEntryCompressData(entry: ZipEntry, data: ByteArray) { + assertWritable() + val compressor = Deflater(compressionLevel, true) compressor.setInput(data) compressor.finish() @@ -136,6 +143,8 @@ class ZipFile(file: File) : Closeable { } private fun addEntryCopyData(entry: ZipEntry, data: ByteBuffer, alignment: Int? = null) { + assertWritable() + alignment?.let { //calculate where data would end up val dataOffset = filePointer.filePointer + entry.LFHSize @@ -162,6 +171,8 @@ class ZipFile(file: File) : Closeable { } fun copyEntriesFromFileAligned(file: ZipFile, entryAlignment: (entry: ZipEntry) -> Int?) { + assertWritable() + for (entry in file.entries) { if (entries.any { it.fileName == entry.fileName }) continue //don't add duplicates