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();
    }

 

posted @ 2017-08-20 13:21  yshy  阅读(805)  评论(0编辑  收藏  举报