public class Compress {
private static final int BUFFER = 2048;
private String[] _files;
private String _zipFile;
public Compress(String[] files, String zipFile) {
_files = files;
_zipFile = zipFile;
}
public void zip() {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
byte data[] = new byte[BUFFER];
for(int i=0; i < _files.length; i++) {
Log.v("Compress", "Adding: " + _files[i]);
FileInputStream fi = new FileInputStream(_files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Call Compress like given below where you want to zip a file :----
String zipFilePath = "fileName.zip";
File zipFile = new File(zipFilePath);
String[] files = new String[] {"/sdcard/fileName"};
if(!zipFile.exists()){
new Compress(files, zipFilePath).zip();
}
您是否嘗試閱讀運行3.x/4.x的Asus或模擬器上的其他zip文件?這個zip文件只有問題嗎? – 2012-05-24 09:11:40
您是否吞嚥任何異常?如果沒有,getName()是否返回zipfile的名稱? (簡單的測試,但可能會出現令人驚訝的事情)。 如果您使用或不使用OPEN_READ標誌打開zip,您會得到不同的結果嗎? – 2012-07-03 16:41:44
我不確定,你可以嘗試用'JarFile'嗎?... – 2012-07-10 13:52:04