我在java下面使用,但是当它压缩时,它会创建一个目录并将该目录中的所有内容压缩。例如。如果我有一个名为“Directory”的文件夹并且我想将内容压缩到一个压缩文件中,则在压缩文件中它会创建一个文件夹 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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我理解正确的话,您想要的是,而不是出现在 Zip 文件中的目标文件夹,其中的所有文件都应以 Zip 文件中的“/”开头。
例如,如果你有
FolderToZip/
TestFile1.txt
TestFile2.txt
TestFile3.txt
SomeSubFolder/
TestFile4.txt
TestFile5.txt
TestFile6.txt
Run Code Online (Sandbox Code Playgroud)
Zip 文件的内容应包含
TestFile1.txt
TestFile2.txt
TestFile3.txt
SomeSubFolder/
TestFile4.txt
TestFile5.txt
TestFile6.txt
Run Code Online (Sandbox Code Playgroud)
为此,您需要保留对“开始”文件夹的引用,并从您添加的文件中删除它的路径,以创建 ZipEntry.
为简单起见,我将您的代码更改为支持File而不是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));
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我还使用try-with-resources清理了您的资源管理
| 归档时间: |
|
| 查看次数: |
5332 次 |
| 最近记录: |