我能够压缩包含在特定文件夹中的文件。这是我使用的代码:
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();
}
}
}
我在另一个类中以这种方式调用这个类:
String[] files = {mainFolderPath+"/text1.txt", mainFolderPath+ "/text2.txt", mainFolderPath +"/NewFolder"};
Compress compress = new Compress(files, sourceFile.getAbsolutePath());
compress.zip();
在运行应用程序时,我得到了一个 IOException。
您能否告诉我如何压缩包含另一个文本文件以及文本文件“text1.txt”和“text2.txt”的“NewFolder”?
谢谢你。