2

我有以下文件夹结构:

根文件夹
|
|
| -->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);
        }
    }
}
4

3 回答 3

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

}

于 2013-02-25T14:14:09.240 回答
1

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

就像是:

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

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

关键是跳过在存档中为根文件夹创建文件夹。

于 2013-02-25T12:05:44.207 回答
1

我编写了一些实用方法来使用 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

于 2013-02-25T12:28:56.420 回答