zoukankan      html  css  js  c++  java
  • java将多个文件的字节数组压缩成一个字节数组,并生成zip文件

    public static void main(String[] args) throws IOException {
    
            Map<String,byte[]> map=new HashMap<>();
            map.put("application.properties",getBytes(new File("F:\shop-mail2\shop-apigateway\src\main\resources\application.properties")));
            map.put("application.yml",getBytes(new File("F:\shop-mail2\shop-apigateway\src\main\resources\application.yml")));
            System.out.println(listBytesToZip(map).length);
    
            String filepath="F:\test\";
            String filename="test.zip";
            fileToBytes(listBytesToZip(map),filepath,filename);
        }
    
        protected static byte[] listBytesToZip(Map<String,byte[]> mapReporte) throws IOException {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
            ZipOutputStream zos = new ZipOutputStream(baos);
    
            for (Map.Entry<String,byte[]> reporte : mapReporte.entrySet()) {
                ZipEntry entry = new ZipEntry(reporte.getKey());
    
                entry.setSize(reporte.getValue().length);
    
                zos.putNextEntry(entry);
    
                zos.write(reporte.getValue());
    
            }
            zos.closeEntry();
            zos.close();
            return baos.toByteArray();
        }
    
        private static byte[] getBytes(File file) throws IOException {
            FileInputStream fis=new FileInputStream(file);
            ByteArrayOutputStream bao=new ByteArrayOutputStream();
            byte[] b=new byte[1024];
            int len=-1;
            while ((len=fis.read(b))!=-1){
                bao.write(b,0,len);
            }
            return bao.toByteArray();
        }
    
        public static void fileToBytes(byte[] bytes, String filePath, String fileName) {
            BufferedOutputStream bos = null;
            FileOutputStream fos = null;
            File file = null;
            try {
    
                file = new File(filePath + fileName);
                if (!file.getParentFile().exists()){
                    //文件夹不存在 生成
                    file.getParentFile().mkdirs();
                }
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos);
                bos.write(bytes);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
  • 相关阅读:
    __proto__、prototype、constructor 之间的关系
    call()与apply()区别
    条件注释判断浏览器
    永远在页面底部的层
    jQuery1.9和jQuery2.0加载(IE10下条件判断失效)
    mousewheel
    2013多校第四场 G题 ZZ的搬砖难题
    hdu 4389 x mod f(x) 数位DP
    hdu 4468 spy 构造kmp
    hdu 4466 triangle 三角形统计 数学计数
  • 原文地址:https://www.cnblogs.com/mibing/p/15150163.html
Copyright © 2011-2022 走看看