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」を作成せずに、「RootFolder」コンテンツ全体を含む zip ファイルを作成する必要があります。

つまり、結果が次のようになる必要があります。

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

NIO ファイル 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 に答える
1

必要なことは、ルート フォルダーではなく、その内容で始まるファイルを追加することです。

何かのようなもの:

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

疑似コードで申し訳ありません。元の関数名を覚えていないため、現在は確認できませんが、簡単にググることができます。

ポイントは、ルート フォルダーのアーカイブにフォルダーを作成することをスキップすることです。

于 2013-02-25T12:05:44.207 に答える
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 に答える