【问题标题】:Zip the root folder content without having the root at zip file压缩根文件夹内容,而 zip 文件中没有根目录
【发布时间】:2013-02-25 11:58:40
【问题描述】:

我有以下文件夹结构:

根文件夹 | | | -->F1-->F1.1-->t1.txt,t2.txt -->F2-->F2.2-->t3.txt

我已经成功了,使用下面的代码,得到下面的zip文件:

result.zip--> 包含:

根文件夹 | | | -->F1-->F1.1-->t1.txt,t2.txt -->F2-->F2.2-->t3.txt

我需要创建一个包含整个“RootFolder”内容的 zip 文件,而无需创建根文件夹“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);
        }
    }
}

【问题讨论】:

    标签: java zip java-io apache-commons-io


    【解决方案1】:

    您需要做的是添加文件,而不是从根文件夹开始,而是从其内容开始。

    类似:

    filelist = getFileList(rootFolder)  
    foreach(File f : filelist){  
        addFolderToZip(f)
    }
    

    抱歉伪代码,不记得原来的函数名称,现在无法检查它们,但可以很容易地用谷歌搜索。

    重点是跳过在存档中为根文件夹创建文件夹。

    【讨论】:

    • 我一直在尝试这样做,但每次我都以这种混乱的算法结束。
    • :我正在尝试找到最简单的解决方案:)
    • 这里是 :FileInputStream in = new FileInputStream(srcFile); String tmp=path.replace(ROOT_FOLDER_NAME, "");
    • 所以我所做的就是将上一行添加到 addFileToZip 方法
    • 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 { 字节[] buf = 新字节[1024];国际化; FileInputStream in = new FileInputStream(srcFile);字符串 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(); } }
    【解决方案2】:

    我已经编写了一些实用方法来使用 NIO File API(该库是开源的)将目录复制到 Zip 文件:

    马文:

    <dependency>  
        <groupId>org.softsmithy.lib</groupId>  
        <artifactId>softsmithy-lib-core</artifactId>  
        <version>0.3</version>  
    </dependency>  
    

    教程:

    http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html#AddZipResourceSample

    API:CopyFileVisitor.copy

    【讨论】:

    • 我读过它,它很好。但是,根据我的阅读,它把容器文件夹以及 zip 文件..我是对的!
    • 不,如果您省略示例中的 2 行并带有注释“将 src 目录名称添加到 zip。如果您只想复制内容,则可以省略它。”
    【解决方案3】:

    我试图找出解决问题的最简单方法。 我解决了它只是删除根目录名称,同时使用以下方法保存在 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();
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 1970-01-01
      • 2013-10-22
      • 2021-01-09
      • 1970-01-01
      • 2012-06-11
      • 2016-01-10
      • 2015-09-07
      • 1970-01-01
      相关资源
      最近更新 更多