Zip目录与java.util.zip和保持结构

时间:2015-08-19 08:23:37

标签: java zip

我正在尝试压缩一个看起来像这样的文件夹:

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的内容。但是,我不知道我的代码中要更改的内容(代码来自一个示例,但我无法找到链接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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我能够通过在方法add 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中的