java如何在安卓中压缩文件夹中包含的多个文件而不压缩整个文件夹?
这是我的要求,我有一个文件夹(比如:主文件夹),其中包含三个项目
One folder and two text files I want to zip only these three items contained in the Main folder .Right now I am zipping the contents with the Main folder and the resultant zipped folder name is "temp.zip",when I unzip this,I am getting the "Main folder". But my requirement is when I unzip the "temp.zip",it should display only the contents of the Main folder. Could any one help me in achieving this? Thank you.
编辑:这是我用来压缩文件的代码 这是我正在压缩文件的代码
public void zipFolder(String srcFolder, String destZipFile)
throws Exception {
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
addFolderToZip("", srcFolder, zip);
zip.flush();
zip.close();
}
private void addFolderToZip(String path, String srcFolder,
ZipOutputStream zip) throws Exception {
File folder = new File(srcFolder);
for (String fileName : folder.list()) {
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
} else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/"
+ fileName, zip);
}
}
}
私有void addFileToZip(字符串路径、字符串srcFile、ZipOutputStream zip) 抛出异常{
File folder = new File(srcFile);
if (folder.isDirectory()) {
addFolderToZip(path, srcFile, zip);
} else {
byte[] buf = new byte[1024];
int len;
FileInputStream in = new FileInputStream(srcFile);
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
}
}
我使用以下参数调用zipfolder方法: zipFolder(srcpolder,destipath+“/”+“temp.zip”)
# 1 楼答案
我刚从这个post抄过来
根据马特的回答,我成功地使用了这个库
# 2 楼答案
在Android中,将一组单独的文件压缩成一个zip应该非常简单。这里有一个很好的教程,可以让你开始学习:
http://www.jondev.net/articles/Zipping_Files_with_Android_%28Programmatically%29
# 3 楼答案
在windows中,可以通过以下步骤实现这一点:
一,。打开主文件夹,选择要添加到zip文件中的文件 2.右键单击->;添加到archieve 3.选择archieve格式为zip,然后单击“确定”
# 4 楼答案