将根文件夹内容压缩成ZIP文件,但不要在ZIP文件中包含根文件夹。

3
我有以下的文件夹结构:
RootFolder
|
|
| -->F1-->F1.1-->t1.txt,t2.txt
  -->F2-->F2.2-->t3.txt
我已经成功地用以下代码获得了下面的zip文件:

result.zip--> 包含:

RootFolder
|
|
| -->F1-->F1.1-->t1.txt,t2.txt
  -->F2-->F2.2-->t3.txt
我需要创建一个zip文件,其中包含整个“RootFolder”内容,但不创建根目录“RootFolder”。
我的意思是,我需要结果像这样:

result.zip--> 包含:

|
|
| -->F1-->F1.1-->t1.txt,t2.txt
  -->F2-->F2.2-->t3.txt
public static void main(String[] args) throws Exception {
    zipFolder("c:/new/RootFolder", "c:/new/result.zip");
}


static public void zipFolder(String srcFolder, String destZipFile)
throws IOException, FileNotFoundException {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

    fileWriter = new FileOutputStream(destZipFile);
    zip = new ZipOutputStream(fileWriter);

    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();
}

static private void addFileToZip(String path, String srcFile,
        ZipOutputStream zip) throws IOException, FileNotFoundException {

    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);
        }
        in.close();
    }
}

    static public void addFolderToZip(String path, String srcFolder,
            ZipOutputStream zip) throws IOException, FileNotFoundException {
        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);
        }
    }
}
3个回答

1
你需要做的是添加文件,不是从根文件夹开始,而是从其内容开始。
类似于:
filelist = getFileList(rootFolder)  
foreach(File f : filelist){  
    addFolderToZip(f)
}

抱歉,这里只有伪代码,我不记得原始函数名,也无法立即检查它们,但是它们可以轻松地通过 Google 搜索获得。
重点是跳过在存档中为根文件夹创建一个文件夹。

我一直在尝试做这件事,但每次都以这个混乱的算法引发异常而告终。 - Echo
我试图寻找最简单的解决方案 :) - Echo
这是相关编程内容的翻译:这里是:FileInputStream in = new FileInputStream(srcFile); String tmp=path.replace(ROOT_FOLDER_NAME, ""); - Echo
所以我所做的就是将前一行添加到addFileToZip方法中。 - Echo
静态私有方法 addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException, FileNotFoundException { 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); String tmp=path.replace(ROOT_FOLDER_NAME, ""); zip.putNextEntry(new ZipEntry(tmp + "/" + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } in.close(); } } - Echo
@Echo 你可以自己看到那完全是无法辨认的。不要在评论中发布大量代码,请将其编辑到你的问题中。 - user207421

1

我已经阅读了它,很好。然而,根据我的阅读,它将容器文件夹以及压缩文件一起放在了zip文件中.. 我是对的吧! - Echo
1
不应该,如果你省略了带有注释“将src目录名添加到zip中。如果只想复制内容,则可以省略此操作。”的示例中的2行代码。 - Puce

1

我试图找到解决问题的最简单方法。我通过以下方式解决了它:在保存zip文件时,只需删除根目录名称即可。

String pathAfterOmittingtheRootFolder=path.replace(ROOT_FOLDER_NAME, "");

完整的方法将是:

static private void addFileToZip(String path, String srcFile,
        ZipOutputStream zip,String exportedRootDirectory) throws IOException, FileNotFoundException {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {

    addFolderToZip(path, srcFile, zip,exportedRootDirectory);
} else {
    byte[] buf = new byte[1024];
    int len;
    FileInputStream in = new FileInputStream(srcFile);
    String pathAfterOmittingtheRootFolder=path.replaceFirst(exportedRootDirectory, "");
    zip.putNextEntry(new ZipEntry(pathAfterOmittingtheRootFolder + "/" + folder.getName()));
    while ((len = in.read(buf)) > 0) {
        zip.write(buf, 0, len);
    }
    in.close();
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接