无法在Android上压缩

时间:2012-05-02 21:08:58

标签: java android zip

我正在尝试在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);

1 个答案:

答案 0 :(得分:1)

以防万一,我会添加closeEntry。

     out.putNextEntry(entry);
     while((count = origin.read(data, 0, BUFFER)) != -1) {
           out.write(data, 0, count);
        }
     origin.close();
     out.closeEntry();