1

これは、3 つのアイテムを含む 1 つのフォルダー (たとえば、メイン フォルダー) がある私の要件です。

1 つのフォルダーと 2 つのテキスト ファイル Main フォルダーに含まれるこれら 3 つのアイテムのみを圧縮したい。 「メインフォルダー」を取得しています。しかし、私の要件は、「temp.zip」を解凍するときに、メインフォルダーの内容のみを表示することです。これを達成するのを手伝ってくれる人はいますか?ありがとうございました。

編集:これは私がファイルを圧縮するために使用しているコードですこれは私がファイルを圧縮しているコードです

public void zipFolder(String srcFolder, String destZipFile)
        throws Exception {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;
    fileWriter = new FileOutputStream(destZipFile);
    zip = new ZipOutputStream(fileWriter);
    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();

}

  private void addFolderToZip(String path, String srcFolder,
        ZipOutputStream zip) throws Exception {
    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);

        }
    }
}                                                                  

private void addFileToZip(String path, String srcFile, ZipOutputStream zip) は例外をスローします {

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

}
}

これらのパラメーターを使用して zipfolder メソッドを呼び出しています: zipFolder(srcfolder, destipath + "/" + "temp.zip");

4

4 に答える 4

0
try {
    String zipFile = "/locations/data.zip";
    String srcFolder = "/locations";

    File folder = new File(srcFolder);
    String[] sourceFiles = folder.list();

    //create byte buffer
    byte[] buffer = new byte[1024];

    /*
     * To create a zip file, use
     *
     * ZipOutputStream(OutputStream out) constructor of ZipOutputStream
     * class.
     */
    //create object of FileOutputStream
    FileOutputStream fout = new FileOutputStream(zipFile);

    //create object of ZipOutputStream from FileOutputStream
    ZipOutputStream zout = new ZipOutputStream(fout);

    for (int i = 0; i < sourceFiles.length; i++) {
        if (sourceFiles[i].equalsIgnoreCase("file.csv") || sourceFiles[i].equalsIgnoreCase("file1.csv")) {
            sourceFiles[i] = srcFolder + fs + sourceFiles[i];
            System.out.println("Adding " + sourceFiles[i]);
            //create object of FileInputStream for source file
            FileInputStream fin = new FileInputStream(sourceFiles[i]);

            /*
             * To begin writing ZipEntry in the zip file, use
             *
             * void putNextEntry(ZipEntry entry) method of
             * ZipOutputStream class.
             *
             * This method begins writing a new Zip entry to the zip
             * file and positions the stream to the start of the entry
             * data.
             */

            zout.putNextEntry(new ZipEntry(sourceFiles[i].substring(sourceFiles[i].lastIndexOf("/") + 1)));

            /*
             * After creating entry in the zip file, actually write the
             * file.
             */
            int length;

            while ((length = fin.read(buffer)) > 0) {
                zout.write(buffer, 0, length);
            }

            /*
             * After writing the file to ZipOutputStream, use
             *
             * void closeEntry() method of ZipOutputStream class to
             * close the current entry and position the stream to write
             * the next entry.
             */

            zout.closeEntry();

            //close the InputStream
            fin.close();

        }
    }

    //close the ZipOutputStream
    zout.close();

    System.out.println("Zip file has been created!");

} catch (IOException ioe) {
    System.out.println("IOException :" + ioe);
}
于 2013-02-06T07:38:08.447 に答える
0

Android で個々のファイルのセットを単一の zip に圧縮することは、非常に簡単です。ここにかなり良いチュートリアルがあります。

http://www.jondev.net/articles/Zipping_Files_with_Android_%28プログラムによる%29

于 2012-09-24T07:28:35.587 に答える
0

この投稿からコピーしただけです

マットの回答から参照して、このライブラリを正常に使用しました。

zip ファイルを処理する純粋な Java ライブラリである Zip4j を試すことができます。PKWare および AES 暗号化方式の暗号化/復号化をサポートします。

http://www.lingala.net/zip4j/

主な機能:

  • Zip ファイルからのファイルの作成、追加、抽出、更新、削除
  • パスワードで保護された Zip ファイルの読み取り/書き込み
  • AES 128/256 暗号化をサポート
  • 標準の Zip 暗号化をサポート
  • Zip64形式をサポート
  • Store (No Compression) および Deflate 圧縮方式をサポート
  • 分割 Zip ファイルからファイルを作成または抽出する (例: z01、z02、...zip)
  • Unicode ファイル名をサポート
  • 進捗モニター

ライセンス:

  • Zip4j は、Apache ライセンス、バージョン 2.0 の下でリリースされています
于 2012-09-24T09:33:56.860 に答える
-2

Windowsでは、次の手順でこれを実現できます。

1.メイン フォルダを開き、zip ファイルに追加するファイルを選択します 2.右クリック -> アーカイブに追加 3.アーカイブ形式を zip として選択し、[OK] をクリックします

于 2012-09-24T06:53:20.993 に答える