Skip to content

Commit 5dacf97

Browse files
author
WindySha
committed
fix asset and res file compressed, cause app crash
1 parent 2cc69f4 commit 5dacf97

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

xpatch/src/main/java/com/storm/wind/xpatch/util/FileUtils.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import java.nio.ByteBuffer;
1616
import java.nio.channels.FileChannel;
1717
import java.nio.charset.Charset;
18+
import java.util.ArrayList;
1819
import java.util.Enumeration;
1920
import java.util.zip.CRC32;
21+
import java.util.zip.CheckedInputStream;
2022
import java.util.zip.CheckedOutputStream;
2123
import java.util.zip.ZipEntry;
2224
import java.util.zip.ZipFile;
@@ -30,6 +32,17 @@ public class FileUtils {
3032

3133
static final int BUFFER = 8192;
3234

35+
// from : http://androidxref.com/9.0.0_r3/xref/frameworks/base/tools/aapt/Package.cpp#30
36+
static final String[] kNoCompressExt = {
37+
".jpg", ".jpeg", ".png", ".gif",
38+
".wav", ".mp2", ".mp3", ".ogg", ".aac",
39+
".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
40+
".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
41+
".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
42+
".amr", ".awb", ".wma", ".wmv",
43+
".tflite", ".lite"
44+
};
45+
3346
/**
3447
* 解压文件
3548
*
@@ -168,7 +181,7 @@ public static void deleteDir(File file) {
168181
file.delete();
169182
}
170183

171-
public static void compressToZip(String srcPath, String dstPath, String originZipPath) {
184+
public static void compressToZip(String srcPath, String dstPath) {
172185
File srcFile = new File(srcPath);
173186
File dstFile = new File(dstPath);
174187
if (!srcFile.exists()) {
@@ -183,7 +196,7 @@ public static void compressToZip(String srcPath, String dstPath, String originZi
183196
CheckedOutputStream cos = new CheckedOutputStream(out, new CRC32());
184197
zipOut = new ZipOutputStream(cos);
185198
String baseDir = "";
186-
compress(srcFile, zipOut, baseDir, true, originZipPath);
199+
compress(srcFile, zipOut, baseDir, true);
187200
} catch (IOException e) {
188201
System.out.println(" compress exception = " + e.getMessage());
189202
} finally {
@@ -199,18 +212,18 @@ public static void compressToZip(String srcPath, String dstPath, String originZi
199212
}
200213
}
201214

202-
private static void compress(File file, ZipOutputStream zipOut, String baseDir, boolean isRootDir, String originZipPath) throws IOException {
215+
private static void compress(File file, ZipOutputStream zipOut, String baseDir, boolean isRootDir) throws IOException {
203216
if (file.isDirectory()) {
204-
compressDirectory(file, zipOut, baseDir, isRootDir, originZipPath);
217+
compressDirectory(file, zipOut, baseDir, isRootDir);
205218
} else {
206-
compressFile(file, zipOut, baseDir, originZipPath);
219+
compressFile(file, zipOut, baseDir);
207220
}
208221
}
209222

210223
/**
211224
* 压缩一个目录
212225
*/
213-
private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir, boolean isRootDir, String originZipPath) throws IOException {
226+
private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir, boolean isRootDir) throws IOException {
214227
File[] files = dir.listFiles();
215228
if (files == null) {
216229
return;
@@ -220,14 +233,14 @@ private static void compressDirectory(File dir, ZipOutputStream zipOut, String b
220233
if (!isRootDir) {
221234
compressBaseDir = baseDir + dir.getName() + "/";
222235
}
223-
compress(files[i], zipOut, compressBaseDir, false, originZipPath);
236+
compress(files[i], zipOut, compressBaseDir, false);
224237
}
225238
}
226239

227240
/**
228241
* 压缩一个文件
229242
*/
230-
private static void compressFile(File file, ZipOutputStream zipOut, String baseDir, String originZipPath) throws IOException {
243+
private static void compressFile(File file, ZipOutputStream zipOut, String baseDir) throws IOException {
231244
if (!file.exists()) {
232245
return;
233246
}
@@ -236,21 +249,24 @@ private static void compressFile(File file, ZipOutputStream zipOut, String baseD
236249
try {
237250
bis = new BufferedInputStream(new FileInputStream(file));
238251
ZipEntry entry = new ZipEntry(baseDir + file.getName());
239-
if (file.getName().contains("resources.arsc")) {
240-
ZipEntry originEntry = getZipEntryFromZipFile(originZipPath, file.getName());
241-
System.out.println(" file name : " + file.getName() + " originEntry = " + originEntry);
242-
if (originEntry != null) {
243-
long size = originEntry.getSize();
244-
long comrepssSize = originEntry.getCompressedSize();
245-
System.out.println(" originEntry = " + originEntry + " size = " + size + " comrepssSize = " + comrepssSize);
246-
if (size == comrepssSize) {
247-
entry.setMethod(ZipEntry.STORED);
248-
entry.setSize(originEntry.getSize());
249-
entry.setCompressedSize(originEntry.getCompressedSize());
250-
entry.setCrc(originEntry.getCrc());
252+
String fileName = file.getName();
253+
boolean isNoCompressFileFormat = false;
254+
int index = fileName.lastIndexOf(".");
255+
if (index >= 0) {
256+
String suffix = fileName.substring(index);
257+
for (String s : kNoCompressExt) {
258+
if (s.equalsIgnoreCase(suffix)) {
259+
isNoCompressFileFormat = true;
260+
break;
251261
}
252262
}
253263
}
264+
if (fileName.equals("resources.arsc") || isNoCompressFileFormat) {
265+
entry.setMethod(ZipEntry.STORED);
266+
entry.setSize(file.length());
267+
long crc = calFileCRC32(file);
268+
entry.setCrc(crc);
269+
}
254270
zipOut.putNextEntry(entry);
255271
int count;
256272
byte data[] = new byte[BUFFER];
@@ -265,6 +281,17 @@ private static void compressFile(File file, ZipOutputStream zipOut, String baseD
265281
}
266282
}
267283

284+
public static long calFileCRC32(File file) throws IOException {
285+
FileInputStream fi = new FileInputStream(file);
286+
CheckedInputStream checksum = new CheckedInputStream(fi, new CRC32());
287+
while (checksum.read() != -1) {
288+
}
289+
long temp = checksum.getChecksum().getValue();
290+
fi.close();
291+
checksum.close();
292+
return temp;
293+
}
294+
268295
private static ZipEntry getZipEntryFromZipFile(String zipPath, String fileName) {
269296
ZipInputStream zin = null;
270297
try {

0 commit comments

Comments
 (0)