20

たとえば、/Users/me/Desktop/image.jpg に保存されているファイルを圧縮したい

私はこの方法を作りました:

public static Boolean generateZipFile(ArrayList<String> sourcesFilenames, String destinationDir, String zipFilename){
  // Create a buffer for reading the files 
  byte[] buf = new byte[1024]; 

  try {
   // VER SI HAY QUE CREAR EL ROOT PATH
         boolean result = (new File(destinationDir)).mkdirs();

         String zipFullFilename = destinationDir + "/" + zipFilename ;

         System.out.println(result);

   // Create the ZIP file  
   ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFullFilename)); 
   // Compress the files 
   for (String filename: sourcesFilenames) { 
    FileInputStream in = new FileInputStream(filename); 
    // Add ZIP entry to output stream. 
    out.putNextEntry(new ZipEntry(filename)); 
    // Transfer bytes from the file to the ZIP file 
    int len; 
    while ((len = in.read(buf)) > 0) { 
     out.write(buf, 0, len); 
    } 
    // Complete the entry 
    out.closeEntry(); 
    in.close(); 
   } // Complete the ZIP file 
   out.close();

   return true;
  } catch (IOException e) { 
   return false;
  }  
 }

しかし、ファイルを抽出すると、解凍されたファイルにはフルパスが含まれています。

zip 内の各ファイルのフル パスは必要ありません。ファイル名だけが必要です。

どうすればこれを作ることができますか?

4

7 に答える 7

33

ここ:

// Add ZIP entry to output stream. 
out.putNextEntry(new ZipEntry(filename)); 

パス全体を使用して、そのファイルのエントリを作成しています。名前だけを (パスなしで) 使用すると、必要なものが得られます。

// Add ZIP entry to output stream. 
File file = new File(filename); //"Users/you/image.jpg"
out.putNextEntry(new ZipEntry(file.getName())); //"image.jpg"
于 2010-06-10T22:40:04.483 に答える
2

ファイルへの相対パスを使用してソースデータを見つけ、エントリを同じものに設定しています。代わりに、ソースを File オブジェクトに変換してから使用する必要があります

putNextEntry(新しい ZipEntry(sourceFile.getName()))

これにより、パスの最後の部分 (つまり、実際のファイル名) だけが得られます。

于 2010-06-10T21:26:33.290 に答える
1

ジェイソンが言ったようにするか、メソッドの署名を保持したい場合は、次のようにします。

out.putNextEntry(new ZipEntry(new File(filename).getName())); 

または、Apache commons/io からFileNameUtils.getNameを使用します。

out.putNextEntry(new ZipEntry(FileNameUtils.getName(filename))); 
于 2010-06-10T21:39:59.243 に答える
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.jpg") || sourceFiles[i].equalsIgnoreCase("file1.jpg")) {
            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:42:19.280 に答える
0
// easy way of zip a file 

java.io.* をインポートします。

import java.util.zip.*;

 public class ZipCreateExample{

    public static void main(String[] args)  throws Exception  {
            // input file 
        FileInputStream in = new FileInputStream("F:/ZipCreateExample.txt");;
        // out put file 
        ZipOutputStream out =new ZipOutputStream(new FileOutputStrea("F:/tmp.zip"));
         // name of file in zip folder 
        out.putNextEntry(new ZipEntry("zippedfile.txt")); 

        byte[] b = new byte[1024];

        int count;

        // writing files to new zippedtxt file
        while ((count = in.read(b)) > 0) {
            System.out.println();

         out.write(b, 0, count);
        }
        out.close();
        in.close();
    }
}
于 2011-12-08T10:14:09.167 に答える
0

おそらく new FileInputStream(new File(sourceFilePath, sourceFileName)) を介してソース ファイルにアクセスすることで回避できます。

于 2010-06-10T21:41:16.017 に答える
0

ただし、同じ名前でパスが異なる 2 つのファイルを圧縮すると、重複ファイル エントリ エラーが発生します。

于 2016-01-26T15:49:12.973 に答える