java中 文件压缩处理

public static void main(String[] args) throws IOException {
        File file=new File("./mhxx_configs.xml");
        File zip=new File("./mhxx.zip");
        String name="configs.txt";                                    //压缩包里文件的名字
        InputStream iStream=new FileInputStream(file);
        FileOutputStream oStream=new FileOutputStream(zip);
        ZipOutputStream zStream=new ZipOutputStream(oStream);
        byte[] a=new byte[iStream.available()];
        zStream.putNextEntry(new ZipEntry(name));                         //一定要有  name为压缩包里文件名
        int read=0;
        while((read=iStream.read(a))>0) {
            zStream.write(a, 0, read);                                   //简单的文件压缩
        }
        
        ByteArrayOutputStream bStream=new ByteArrayOutputStream();       
        ZipOutputStream zStream2=new ZipOutputStream(bStream);
        zStream2.putNextEntry(new ZipEntry(name));
        zStream2.write(a);
        a=bStream.toByteArray();                                         //把文件压缩为byte[]    
        for(byte b:a) {
            System.out.print(b+" ");
        }
        
        
        zStream.closeEntry();                                           //流一定要记得关 不关  结果各种操蛋
        zStream.close();
        zStream2.close();
        iStream.close();
        oStream.close();
        bStream.close();

 

posted @ 2017-10-27 16:07  七千米蔚蓝  阅读(324)  评论(0编辑  收藏  举报