gpt4 book ai didi

java - 如何在android中压缩一个文件夹中包含的多个文件而不压缩整个文件夹?

转载 作者:行者123 更新时间:2023-12-01 06:19:47 24 4
gpt4 key购买 nike

这是我的要求,我有一个文件夹(例如:主文件夹),其中包含三个项目

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);

}
}
}

private 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(srcfolder, 目的地路径 + "/"+ "temp.zip");

最佳答案

在 Android 中将一组单独的文件压缩到一个 zip 中应该非常简单。这里有一个非常好的教程,应该可以帮助您入门:

http://www.jondev.net/articles/Zipping_Files_with_Android_%28Programmatically%29

关于java - 如何在android中压缩一个文件夹中包含的多个文件而不压缩整个文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12559436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com