Java 生成ZIP文件

 public static byte[] fileToZip(){
        ZipOutputStream append = null;
        
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        
        try {
            append = new ZipOutputStream(bos);
            
            ZipEntry e = new ZipEntry("request.xml");
            append.putNextEntry(e);
            append.write(filteToByte("G:\tmp\request.xml"));
            append.closeEntry();
            
            e = new ZipEntry("CONTENT\content.xml");
            append.putNextEntry(e);
            append.write(filteToByte("G:\tmp\content.xml"));
            append.closeEntry();
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            if(null != append){
                try {
                    append.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return bos.toByteArray();
    }
原文地址:https://www.cnblogs.com/yshyee/p/7399713.html