zoukankan      html  css  js  c++  java
  • 大数据之文件的压缩和解压缩

    一,压缩

    类似于war提供的功能;实现由ZipOutputStream类和ZipInputStream提供

    public static void addFile(ZipOutputStream zos,String path) throws Exception
        {
            File f = new File(path);
            zos.putNextEntry(new ZipEntry(f.getName()));
            FileInputStream fis = new FileInputStream(f);
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            fis.close();
            
            zos.write(bytes);
            zos.closeEntry();
        }

    二,解压缩

    public void unzip() throws Exception
        {
            FileInputStream fis = new FileInputStream("d:/arch/xxx.zip");
            ZipInputStream zis = new ZipInputStream(fis);
            
            ZipEntry entry = null;
            byte[] buf = new byte[1024];
            int len = 0;
            while((entry = zis.getNextEntry())!=null){
                String name = entry.getName();
                FileOutputStream fos = new FileOutputStream("d:/arch/unzip/"+name);
                while((len = zis.read(buf))!= -1)
                {
                    fos.write(buf,0,len);
                }
                fos.close();
            }
            zis.close();
            fis.close();
            
        }

    类的调用;

    public static void main(String[] args) throws Exception {
             FileOutputStream fos = new FileOutputStream("d:/arch/xxx.xar");
             ZipOutputStream zos = new ZipOutputStream(fos);
             
             String[] arr = {
                     "d:/arch/a.xls",
                     "d:/arch/c.txt"
             };
             
            for(String s:arr){
                addFile(zos,s);
            }
            zos.close();
            fos.close();
            System.out.println("over");
        }
  • 相关阅读:
    Linux03__管理
    Linux02__常用命令
    Linux01__系统安装
    爬虫性能相关
    【转载】资源整合
    Continuous integration
    行业巨头的云计算冷数据存储应用和比较 2016-07-15
    win7中使用docker ——配置阿里云容器加速
    layui treeSelect插件的使用
    springboot 拦截器设置
  • 原文地址:https://www.cnblogs.com/ithome0222/p/8735053.html
Copyright © 2011-2022 走看看