5
所以我創建了一個類,在這裏找到了這些例子。 http://www.jondev.net/articles/Zipping_Files_with_Android_%28Programmatically%29這是偉大的,它創造奇蹟,文件。說到文件夾,它不起作用。我嘗試將字符串切換到文件並使用isDirectory,但這是不行的。這裏是代碼:在android中對文件和文件夾進行壓縮
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
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();
}
}
}
如果你設法使它壓縮文件夾也請聯繫我,我有同樣的問題,並沒有管理尚未 –
試試這個http://stackoverflow.com/questions/1399126/java-util-zip-重新創建目錄結構 –