java - 如何在Java中压缩目录的内容
问题描述
我在下面使用java,但是当它压缩时,它会创建一个目录并将该目录中的所有内容压缩。例如。如果我有一个名为“目录”的文件夹,并且我想将内容压缩到一个压缩文件中,则在压缩文件中它会创建一个文件夹 testZip 并在其中包含文件。我需要压缩文件中的所有文件,而不是父目录中的所有文件。请帮忙。或建议是否有其他方法。
package folderZip;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipFolder {
public ZipFolder() {
}
public static void main(String[] args) throws Exception {
ZipFolder obj = new ZipFolder();
obj.zipFolder("C:\\Drive\\temp\\testZip","C:\\Drive\\temp\\FolderZiper.zip");
}
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 addFileToZip(String path, String srcFile,
ZipOutputStream zip) throws Exception {
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);
}
}
}
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);
}
}
}
}
解决方案
所以,如果我理解正确,你想要的是,而不是出现在 Zip 文件中的目标文件夹,其中的所有文件都应该从 Zip 文件中的“/”开始。
例如,如果你有
FolderToZip/
TestFile1.txt
TestFile2.txt
TestFile3.txt
SomeSubFolder/
TestFile4.txt
TestFile5.txt
TestFile6.txt
Zip 文件的内容应包含
TestFile1.txt
TestFile2.txt
TestFile3.txt
SomeSubFolder/
TestFile4.txt
TestFile5.txt
TestFile6.txt
为此,您需要保留对“开始”文件夹的引用,并将其路径从您添加的文件中剥离,以创建ZipEntry
.
为简单起见,我将您的代码更改为 supportFile
而不是String
,它只是让整个事情变得不那么混乱。
但是魔法出现在这里……
FileInputStream in = new FileInputStream(srcFile);
String name = srcFile.getPath();
name = name.replace(rootPath.getPath(), "");
System.out.println("Zip " + srcFile + "\n to " + name);
zip.putNextEntry(new ZipEntry(name));
rootPath
是初始文件夹(C:\\Drive\\temp\\testZip"
在您的示例中),srcFile
是要添加的文件。
可运行的示例...
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipFolder {
public ZipFolder() {
}
public static void main(String[] args) throws Exception {
ZipFolder obj = new ZipFolder();
obj.zipFolder(new File("/Users/shanewhitehead/exports"),
new File("FolderZiper.zip"));
}
public void zipFolder(File srcFolder, File destZipFile) throws Exception {
try (FileOutputStream fileWriter = new FileOutputStream(destZipFile);
ZipOutputStream zip = new ZipOutputStream(fileWriter)) {
addFolderToZip(srcFolder, srcFolder, zip);
}
}
private void addFileToZip(File rootPath, File srcFile, ZipOutputStream zip) throws Exception {
if (srcFile.isDirectory()) {
addFolderToZip(rootPath, srcFile, zip);
} else {
byte[] buf = new byte[1024];
int len;
try (FileInputStream in = new FileInputStream(srcFile)) {
String name = srcFile.getPath();
name = name.replace(rootPath.getPath(), "");
System.out.println("Zip " + srcFile + "\n to " + name);
zip.putNextEntry(new ZipEntry(name));
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
}
}
}
private void addFolderToZip(File rootPath, File srcFolder, ZipOutputStream zip) throws Exception {
for (File fileName : srcFolder.listFiles()) {
addFileToZip(rootPath, fileName, zip);
}
}
}
我还使用try-with-resources清理了您的资源管理
推荐阅读
- typescript - 您可以将 for in 循环中的键转换为接口的属性吗?
- html - 无法让图像链接出现在水平线上
- php - 如何以预设尺寸(800*400px)裁剪图像的选定部分?
- spring-boot - 反应式 LdapTemplate 的可用性
- c++ - 正在解压缩和提取空文件
- c++ - 克隆使用 vcpkg 的项目时如何安装软件包
- python - Python连接字节问题(二)
- javascript - TypeError:无法读取 Angular 8 中未定义的属性“sendNotification”
- dll - 如何让 gnatmake 为 Ada .dll 创建映射文件?
- npm - 错误代码:135 - 在 package.json 中调用 npm run protractor 时 ECONNREFUSED