1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.documentsui.archives;
18 
19 import android.text.TextUtils;
20 
21 import androidx.annotation.NonNull;
22 
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.io.InputStream;
26 
27 import org.apache.commons.compress.archivers.ArchiveEntry;
28 import org.apache.commons.compress.archivers.ArchiveInputStream;
29 import org.apache.commons.compress.archivers.sevenz.SevenZFile;
30 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
31 import org.apache.commons.compress.archivers.zip.ZipFile;
32 
33 /**
34  * To simulate the input stream by using ZipFile, SevenZFile, or ArchiveInputStream.
35  */
36 abstract class ArchiveEntryInputStream extends InputStream {
37     private final long mSize;
38     private final ReadSource mReadSource;
39 
ArchiveEntryInputStream(ReadSource readSource, @NonNull ArchiveEntry archiveEntry)40     private ArchiveEntryInputStream(ReadSource readSource, @NonNull ArchiveEntry archiveEntry) {
41         mReadSource = readSource;
42         mSize = archiveEntry.getSize();
43     }
44 
45     @Override
read()46     public int read() throws IOException {
47         throw new UnsupportedOperationException();
48     }
49 
50     @Override
read(byte[] b, int off, int len)51     public int read(byte[] b, int off, int len) throws IOException {
52         if (mReadSource != null) {
53             return mReadSource.read(b, off, len);
54         }
55 
56         return -1; /* end of input stream */
57     }
58 
59     @Override
available()60     public int available() throws IOException {
61         return mReadSource == null ? 0 : StrictMath.toIntExact(mSize);
62     }
63 
64     /**
65      * The interface describe how to make the ArchiveHandle to next entry.
66      */
67     private interface NextEntryIterator {
getNextEntry()68         ArchiveEntry getNextEntry() throws IOException;
69     }
70 
71     /**
72      * The interface provide where to read the data.
73      */
74     private interface ReadSource {
read(byte[] b, int off, int len)75         int read(byte[] b, int off, int len) throws IOException;
76     }
77 
moveToArchiveEntry( NextEntryIterator nextEntryIterator, ArchiveEntry archiveEntry)78     private static boolean moveToArchiveEntry(
79             NextEntryIterator nextEntryIterator, ArchiveEntry archiveEntry) throws IOException {
80         ArchiveEntry entry;
81         while ((entry = nextEntryIterator.getNextEntry()) != null) {
82             if (TextUtils.equals(entry.getName(), archiveEntry.getName())) {
83                 return true;
84             }
85         }
86         return false;
87     }
88 
89     private static class WrapArchiveInputStream extends ArchiveEntryInputStream {
WrapArchiveInputStream(ReadSource readSource, ArchiveEntry archiveEntry, NextEntryIterator iterator)90         private WrapArchiveInputStream(ReadSource readSource,
91                 ArchiveEntry archiveEntry, NextEntryIterator iterator) throws IOException {
92             super(readSource, archiveEntry);
93 
94             moveToArchiveEntry(iterator, archiveEntry);
95         }
96     }
97 
98     private static class WrapZipFileInputStream extends ArchiveEntryInputStream {
99         private final Closeable mCloseable;
100 
WrapZipFileInputStream( ReadSource readSource, @NonNull ArchiveEntry archiveEntry, Closeable closeable)101         private WrapZipFileInputStream(
102                 ReadSource readSource, @NonNull ArchiveEntry archiveEntry, Closeable closeable)
103                 throws IOException {
104             super(readSource, archiveEntry);
105             mCloseable = closeable;
106         }
107 
108         @Override
close()109         public void close() throws IOException {
110             super.close();
111             if (mCloseable != null) {
112                 mCloseable.close();
113             }
114         }
115     }
116 
create(@onNull ArchiveHandle archiveHandle, @NonNull ArchiveEntry archiveEntry)117     static InputStream create(@NonNull ArchiveHandle archiveHandle,
118             @NonNull ArchiveEntry archiveEntry) throws IOException {
119         if (archiveHandle == null) {
120             throw new IllegalArgumentException("archiveHandle is null");
121         }
122 
123         if (archiveEntry == null) {
124             throw new IllegalArgumentException("ArchiveEntry is empty");
125         }
126 
127         if (archiveEntry.isDirectory() || archiveEntry.getSize() <= 0
128                 || TextUtils.isEmpty(archiveEntry.getName())) {
129             throw new IllegalArgumentException("ArchiveEntry is an invalid file entry");
130         }
131 
132         Object commonArchive = archiveHandle.getCommonArchive();
133 
134         if (commonArchive instanceof SevenZFile) {
135             return new WrapArchiveInputStream(
136                 (b, off, len) -> ((SevenZFile) commonArchive).read(b, off, len),
137                 archiveEntry,
138                 () -> ((SevenZFile) commonArchive).getNextEntry());
139         } else if (commonArchive instanceof ZipFile) {
140             final InputStream inputStream =
141                     ((ZipFile) commonArchive).getInputStream((ZipArchiveEntry) archiveEntry);
142             return new WrapZipFileInputStream(
143                 (b, off, len) -> inputStream.read(b, off, len),
144                 archiveEntry,
145                 () -> inputStream.close());
146         } else if (commonArchive instanceof ArchiveInputStream) {
147             return new WrapArchiveInputStream(
148                 (b, off, len) -> ((ArchiveInputStream) commonArchive).read(b, off, len),
149                 archiveEntry,
150                 () -> ((ArchiveInputStream) commonArchive).getNextEntry());
151         }
152 
153         return null;
154     }
155 }
156