From 673735ab2e95d56dc0c0ec74f724139c2a6a33e8 Mon Sep 17 00:00:00 2001 From: seven332 Date: Mon, 14 Jan 2019 00:55:06 +0800 Subject: [PATCH] Update a7zip --- app/build.gradle | 2 +- .../com/hippo/ehviewer/EhApplication.java | 4 +- .../hippo/ehviewer/gallery/A7ZipArchive.java | 64 +++++++++++- .../java/okio/UniRandomAccessFileStore.java | 99 ------------------- 4 files changed, 63 insertions(+), 106 deletions(-) delete mode 100644 app/src/main/java/okio/UniRandomAccessFileStore.java diff --git a/app/build.gradle b/app/build.gradle index ad9f2cb55..214a038ef 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -72,7 +72,7 @@ dependencies { implementation 'com.google.firebase:firebase-core:16.0.6' implementation 'com.crashlytics.sdk.android:crashlytics:2.9.8' implementation 'com.github.amlcurran.showcaseview:library:5.4.3' - implementation 'com.github.seven332:a7zip:96076610fc' + implementation 'com.github.seven332.a7zip:extract-lite:d4ddec5793' implementation 'com.github.seven332:android-recaptcha:2bbb3459a8' implementation 'com.github.seven332:android-resource:0.1.0' implementation 'com.github.seven332:animator:0.1.0' diff --git a/app/src/main/java/com/hippo/ehviewer/EhApplication.java b/app/src/main/java/com/hippo/ehviewer/EhApplication.java index b54af5e2b..8da7105b8 100644 --- a/app/src/main/java/com/hippo/ehviewer/EhApplication.java +++ b/app/src/main/java/com/hippo/ehviewer/EhApplication.java @@ -31,7 +31,9 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.util.LruCache; import android.util.Log; +import com.getkeepsafe.relinker.ReLinker; import com.hippo.a7zip.A7Zip; +import com.hippo.a7zip.A7ZipExtractLite; import com.hippo.beerbelly.SimpleDiskCache; import com.hippo.conaco.Conaco; import com.hippo.content.RecordingApplication; @@ -105,7 +107,7 @@ public class EhApplication extends RecordingApplication { EhEngine.initialize(); BitmapUtils.initialize(this); Image.initialize(this); - A7Zip.initialize(this); + A7Zip.loadLibrary(A7ZipExtractLite.LIBRARY, libname -> ReLinker.loadLibrary(EhApplication.this, libname)); if (EhDB.needMerge()) { EhDB.mergeOldDB(this); diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/A7ZipArchive.java b/app/src/main/java/com/hippo/ehviewer/gallery/A7ZipArchive.java index 85e838b9c..72529c135 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/A7ZipArchive.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/A7ZipArchive.java @@ -18,15 +18,16 @@ package com.hippo.ehviewer.gallery; import com.hippo.a7zip.ArchiveException; import com.hippo.a7zip.InArchive; +import com.hippo.a7zip.InStream; import com.hippo.a7zip.PropID; import com.hippo.a7zip.PropType; +import com.hippo.a7zip.SequentialOutStream; import com.hippo.unifile.UniRandomAccessFile; import java.io.Closeable; +import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; -import okio.Store; -import okio.UniRandomAccessFileStore; class A7ZipArchive implements Closeable { @@ -69,8 +70,8 @@ class A7ZipArchive implements Closeable { } static A7ZipArchive create(UniRandomAccessFile file) throws ArchiveException { - Store store = new UniRandomAccessFileStore(file); - InArchive archive = InArchive.create(store); + InStream store = new UniRandomAccessFileInStream(file); + InArchive archive = InArchive.open(store); if ((archive.getArchivePropertyType(PropID.ENCRYPTED) == PropType.BOOL && archive.getArchiveBooleanProperty(PropID.ENCRYPTED)) || (archive.getArchivePropertyType(PropID.SOLID) == PropType.BOOL && archive.getArchiveBooleanProperty(PropID.SOLID)) || (archive.getArchivePropertyType(PropID.IS_VOLUME) == PropType.BOOL && archive.getArchiveBooleanProperty(PropID.IS_VOLUME))) { @@ -96,7 +97,60 @@ class A7ZipArchive implements Closeable { } void extract(OutputStream os) throws ArchiveException { - archive.extractEntry(index, os); + archive.extractEntry(index, new OutputStreamSequentialOutStream(os)); + } + } + + private static class UniRandomAccessFileInStream implements InStream { + + private UniRandomAccessFile file; + + public UniRandomAccessFileInStream(UniRandomAccessFile file) { + this.file = file; + } + + @Override + public void seek(long pos) throws IOException { + file.seek(pos); + } + + @Override + public long tell() throws IOException { + return file.getFilePointer(); + } + + @Override + public long size() throws IOException { + return file.length(); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + return file.read(b, off, len); + } + + @Override + public void close() throws IOException { + file.close(); + } + } + + private static class OutputStreamSequentialOutStream implements SequentialOutStream { + + private OutputStream stream; + + public OutputStreamSequentialOutStream(OutputStream stream) { + this.stream = stream; + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + stream.write(b, off, len); + } + + @Override + public void close() throws IOException { + stream.close(); } } } diff --git a/app/src/main/java/okio/UniRandomAccessFileStore.java b/app/src/main/java/okio/UniRandomAccessFileStore.java deleted file mode 100644 index 5afedd7de..000000000 --- a/app/src/main/java/okio/UniRandomAccessFileStore.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2018 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package okio; - -import static okio.Util.checkOffsetAndCount; - -import com.hippo.unifile.UniRandomAccessFile; -import java.io.IOException; - -public class UniRandomAccessFileStore implements Store { - - private UniRandomAccessFile file; - private Timeout timeout; - - public UniRandomAccessFileStore(UniRandomAccessFile file) { - this(file, new Timeout()); - } - - public UniRandomAccessFileStore(UniRandomAccessFile file, Timeout timeout) { - this.file = file; - this.timeout = timeout; - } - - @Override - public void seek(long position) throws IOException { - file.seek(position); - } - - @Override - public long tell() throws IOException { - return file.getFilePointer(); - } - - @Override - public long size() throws IOException { - return file.length(); - } - - @Override - public long read(Buffer sink, long byteCount) throws IOException { - if (byteCount < 0) throw new IllegalArgumentException("byteCount < 0: " + byteCount); - if (byteCount == 0) return 0; - timeout.throwIfReached(); - Segment tail = sink.writableSegment(1); - int maxToCopy = (int) Math.min(byteCount, Segment.SIZE - tail.limit); - int bytesRead = file.read(tail.data, tail.limit, maxToCopy); - if (bytesRead == -1) return -1; - tail.limit += bytesRead; - sink.size += bytesRead; - return bytesRead; - } - - @Override - public void write(Buffer source, long byteCount) throws IOException { - checkOffsetAndCount(source.size, 0, byteCount); - while (byteCount > 0) { - timeout.throwIfReached(); - Segment head = source.head; - int toCopy = (int) Math.min(byteCount, head.limit - head.pos); - file.write(head.data, head.pos, toCopy); - - head.pos += toCopy; - byteCount -= toCopy; - source.size -= toCopy; - - if (head.pos == head.limit) { - source.head = head.pop(); - SegmentPool.recycle(head); - } - } - } - - @Override - public void flush() { } - - @Override - public Timeout timeout() { - return timeout; - } - - @Override - public void close() throws IOException { - file.close(); - } -}