15
15
import java .nio .ByteBuffer ;
16
16
import java .nio .channels .FileChannel ;
17
17
import java .nio .charset .Charset ;
18
+ import java .util .ArrayList ;
18
19
import java .util .Enumeration ;
19
20
import java .util .zip .CRC32 ;
21
+ import java .util .zip .CheckedInputStream ;
20
22
import java .util .zip .CheckedOutputStream ;
21
23
import java .util .zip .ZipEntry ;
22
24
import java .util .zip .ZipFile ;
@@ -30,6 +32,17 @@ public class FileUtils {
30
32
31
33
static final int BUFFER = 8192 ;
32
34
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
+
33
46
/**
34
47
* 解压文件
35
48
*
@@ -168,7 +181,7 @@ public static void deleteDir(File file) {
168
181
file .delete ();
169
182
}
170
183
171
- public static void compressToZip (String srcPath , String dstPath , String originZipPath ) {
184
+ public static void compressToZip (String srcPath , String dstPath ) {
172
185
File srcFile = new File (srcPath );
173
186
File dstFile = new File (dstPath );
174
187
if (!srcFile .exists ()) {
@@ -183,7 +196,7 @@ public static void compressToZip(String srcPath, String dstPath, String originZi
183
196
CheckedOutputStream cos = new CheckedOutputStream (out , new CRC32 ());
184
197
zipOut = new ZipOutputStream (cos );
185
198
String baseDir = "" ;
186
- compress (srcFile , zipOut , baseDir , true , originZipPath );
199
+ compress (srcFile , zipOut , baseDir , true );
187
200
} catch (IOException e ) {
188
201
System .out .println (" compress exception = " + e .getMessage ());
189
202
} finally {
@@ -199,18 +212,18 @@ public static void compressToZip(String srcPath, String dstPath, String originZi
199
212
}
200
213
}
201
214
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 {
203
216
if (file .isDirectory ()) {
204
- compressDirectory (file , zipOut , baseDir , isRootDir , originZipPath );
217
+ compressDirectory (file , zipOut , baseDir , isRootDir );
205
218
} else {
206
- compressFile (file , zipOut , baseDir , originZipPath );
219
+ compressFile (file , zipOut , baseDir );
207
220
}
208
221
}
209
222
210
223
/**
211
224
* 压缩一个目录
212
225
*/
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 {
214
227
File [] files = dir .listFiles ();
215
228
if (files == null ) {
216
229
return ;
@@ -220,14 +233,14 @@ private static void compressDirectory(File dir, ZipOutputStream zipOut, String b
220
233
if (!isRootDir ) {
221
234
compressBaseDir = baseDir + dir .getName () + "/" ;
222
235
}
223
- compress (files [i ], zipOut , compressBaseDir , false , originZipPath );
236
+ compress (files [i ], zipOut , compressBaseDir , false );
224
237
}
225
238
}
226
239
227
240
/**
228
241
* 压缩一个文件
229
242
*/
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 {
231
244
if (!file .exists ()) {
232
245
return ;
233
246
}
@@ -236,21 +249,24 @@ private static void compressFile(File file, ZipOutputStream zipOut, String baseD
236
249
try {
237
250
bis = new BufferedInputStream (new FileInputStream (file ));
238
251
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 ;
251
261
}
252
262
}
253
263
}
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
+ }
254
270
zipOut .putNextEntry (entry );
255
271
int count ;
256
272
byte data [] = new byte [BUFFER ];
@@ -265,6 +281,17 @@ private static void compressFile(File file, ZipOutputStream zipOut, String baseD
265
281
}
266
282
}
267
283
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
+
268
295
private static ZipEntry getZipEntryFromZipFile (String zipPath , String fileName ) {
269
296
ZipInputStream zin = null ;
270
297
try {
0 commit comments