1

我正在尝试在 Android 上使用 zip 进行压缩,但它不起作用。它确实创建了一个 3KB 大小的 zip 文件夹,仅此而已。我做错了什么?

public static void compress(String infile, String outfile){
    try {
         BufferedInputStream origin = null;
         FileOutputStream dest = new FileOutputStream(outfile);
         ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
         byte data[] = new byte[BUFFER];
         File f = new File(infile);
         Log.d(TAG, "Only one file found");
         FileInputStream fi = new FileInputStream(f);
         Log.d(TAG, "File Input Stream available " + fi.available());
         origin = new BufferedInputStream(fi, BUFFER);
         ZipEntry entry = new ZipEntry(infile);
         int count;
         out.putNextEntry(entry);
         while((count = origin.read(data, 0, BUFFER)) != -1) {
               out.write(data, 0, count);
            }
         origin.close();


         out.close();
      } catch(Exception e) {
         e.printStackTrace();
      }

}

而我的调用函数是这样的

String outFile="/sdcard/CPTA/test.zip";
String inFile="/sdcard/CPTA/iso_3166-1_list_en.xml";
ZipUtility.compress(inFile, outFile);
4

1 回答 1

1

以防万一,我会添加 closeEntry。

     out.putNextEntry(entry);
     while((count = origin.read(data, 0, BUFFER)) != -1) {
           out.write(data, 0, count);
        }
     origin.close();
     out.closeEntry();
于 2012-05-02T22:18:33.363 回答