// Zip Test
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class SimpleZipTest {    public static void main(String[] args) {
        try {
            File zipFile = new File("E:\\test.zip");
            File beZippedFile = new File("E:\\forTest.dat");
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
            zipFile(beZippedFile, zos);
            zos.flush();
            zos.close();
            System.out.println("Finish...");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    static void zipFile(File file, ZipOutputStream zos) throws FileNotFoundException, IOException {
        byte[] readBuffer = new byte[2156];
        int bytesIn = 0;
        FileInputStream fis = new FileInputStream(file);
        ZipEntry anEntry = new ZipEntry(file.getName());
        anEntry.setSize(fis.available());
        zos.putNextEntry(anEntry);
        while ((bytesIn = fis.read(readBuffer)) != -1) {
            zos.write(readBuffer, 0, bytesIn);
        }
        fis.close();
    }    
}

解决方案 »

  1.   

    // Unzip Testimport java.io.FileInputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;public class SimpleUnzipTest {    public static void main(String[] args) {
            String zipFile = new String("E:\\test.zip");
            getExtractFileLength(zipFile);        
        }
        
        static void getExtractFileLength(String zipFilePath) {       
            try {
                FileInputStream inputstream = new FileInputStream(zipFilePath);
                ZipInputStream zipinputstreamForLength = new ZipInputStream(inputstream);
                ZipEntry zipentryForLength = null;            while ((zipentryForLength = zipinputstreamForLength.getNextEntry()) != null) {
                    System.out.println(zipentryForLength.getSize());
                }
                zipinputstreamForLength.close();
                inputstream.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }        
        }
    }
      

  2.   

    是不是打包没成功,用winrar打开有文件吗?
      

  3.   

    http://community.csdn.net/Expert/TopicView1.asp?id=3222720
      

  4.   

    To rocshaw:
    http://community.csdn.net/Expert/TopicView1.asp?id=3222720
    这个链接和本文看似无关啊To Gwyongcheng:
    打包肯定成功了,没有抛出任何异常,用WinRAR和WinZip都可以正常打开