带有 java.util.zip 并保持结构的 Zip 目录
Zip directory with java.util.zip and keeping structure
我正在尝试压缩一个看起来像这样的文件夹:
Folder1
file.xml
file.xml
folder2
file.txt
当我用我的 Java 程序压缩它时,它看起来像这样:
Folder1.zip
Folder1 (i dont want hist directory)
file.xml
file.xml
folder2
file.txt
我需要直接在 folder1.zip 中获取 folder1 的内容。但是,我不知道我的代码要更改什么(代码来自示例,但我找不到 link atm)。
/*
* Zip function zip all files and folders
*/
@SuppressWarnings("finally")
public boolean zipFiles(String srcFolder, String destZipFile) {
boolean result = false;
try {
logger.info("Program Start zipping the given files");
/*
* send to the zip procedure
*/
zipFolder(srcFolder, destZipFile);
result = true;
logger.info("Given files are successfully zipped");
} catch (Exception e) {
System.out.println("Some Errors happned during the zip process");
} finally {
return result;
}
}
/*
* zip the folders
*/
private void zipFolder(String srcFolder, String destZipFile) throws Exception {
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
/*
* create the output stream to zip file result
*/
fileWriter = new FileOutputStream(destZipFile + ".zip");
zip = new ZipOutputStream(fileWriter);
/*
* add the folder to the zip
*/
addFolderToZip("", srcFolder, zip);
/*
* close the zip objects
*/
zip.flush();
zip.close();
}
/*
* recursively add files to the zip files
*/
private void addFileToZip(String path, String srcFile, ZipOutputStream zip, boolean flag) throws Exception {
/*
* create the file object for inputs
*/
File folder = new File(srcFile);
/*
* if the folder is empty add empty folder to the Zip file
*/
if (flag == true) {
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName() + "/"));
} else { /*
* if the current name is directory, recursively traverse it
* to get the files
*/
if (folder.isDirectory()) {
/*
* if folder is not empty
*/
addFolderToZip(path, srcFile, zip);
} else {
/*
* write the file to the output
*/
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) {
/*
* Write the Result
*/
zip.write(buf, 0, len);
}
in.close();
}
}
}
/*
* add folder to the zip file
*/
private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception {
File folder = new File(srcFolder);
/*
* check the empty folder
*/
if (folder.list().length == 0) {
logger.info(folder.getName());
addFileToZip(path, srcFolder, zip, true);
} else {
/*
* list the files in the folder
*/
for (String fileName : folder.list()) {
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false);
} else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false);
}
}
}
}
我能够通过在添加 FileToZip 的方法中包含这行代码来解决问题
if (folder.getAbsolutePath().substring(folder.getAbsolutePath().indexOf(".")).equals(".xml")) {
// save xml File in root directory
zip.putNextEntry(new ZipEntry(folder.getName()));
} else {
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
}
和
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false);
} else {
if (rootDirectory) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false);
} else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false);
}
}
在方法 addFolderToZip 中。
我正在尝试压缩一个看起来像这样的文件夹:
Folder1
file.xml
file.xml
folder2
file.txt
当我用我的 Java 程序压缩它时,它看起来像这样:
Folder1.zip
Folder1 (i dont want hist directory)
file.xml
file.xml
folder2
file.txt
我需要直接在 folder1.zip 中获取 folder1 的内容。但是,我不知道我的代码要更改什么(代码来自示例,但我找不到 link atm)。
/*
* Zip function zip all files and folders
*/
@SuppressWarnings("finally")
public boolean zipFiles(String srcFolder, String destZipFile) {
boolean result = false;
try {
logger.info("Program Start zipping the given files");
/*
* send to the zip procedure
*/
zipFolder(srcFolder, destZipFile);
result = true;
logger.info("Given files are successfully zipped");
} catch (Exception e) {
System.out.println("Some Errors happned during the zip process");
} finally {
return result;
}
}
/*
* zip the folders
*/
private void zipFolder(String srcFolder, String destZipFile) throws Exception {
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
/*
* create the output stream to zip file result
*/
fileWriter = new FileOutputStream(destZipFile + ".zip");
zip = new ZipOutputStream(fileWriter);
/*
* add the folder to the zip
*/
addFolderToZip("", srcFolder, zip);
/*
* close the zip objects
*/
zip.flush();
zip.close();
}
/*
* recursively add files to the zip files
*/
private void addFileToZip(String path, String srcFile, ZipOutputStream zip, boolean flag) throws Exception {
/*
* create the file object for inputs
*/
File folder = new File(srcFile);
/*
* if the folder is empty add empty folder to the Zip file
*/
if (flag == true) {
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName() + "/"));
} else { /*
* if the current name is directory, recursively traverse it
* to get the files
*/
if (folder.isDirectory()) {
/*
* if folder is not empty
*/
addFolderToZip(path, srcFile, zip);
} else {
/*
* write the file to the output
*/
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) {
/*
* Write the Result
*/
zip.write(buf, 0, len);
}
in.close();
}
}
}
/*
* add folder to the zip file
*/
private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception {
File folder = new File(srcFolder);
/*
* check the empty folder
*/
if (folder.list().length == 0) {
logger.info(folder.getName());
addFileToZip(path, srcFolder, zip, true);
} else {
/*
* list the files in the folder
*/
for (String fileName : folder.list()) {
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false);
} else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false);
}
}
}
}
我能够通过在添加 FileToZip 的方法中包含这行代码来解决问题
if (folder.getAbsolutePath().substring(folder.getAbsolutePath().indexOf(".")).equals(".xml")) {
// save xml File in root directory
zip.putNextEntry(new ZipEntry(folder.getName()));
} else {
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
}
和
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false);
} else {
if (rootDirectory) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false);
} else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false);
}
}
在方法 addFolderToZip 中。