zoukankan      html  css  js  c++  java
  • Java zip and unzip demo

    目录结构如下:

    image

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipOutputStream;
    
    public class zipDemo {
    
        public static void main(String[] args)  {
         try {
            //zipFolder("/home/hadoop/test";);
            unzip("/home/hadoop/mytest/test.zip","/home/hadoop/mytest/");
        } catch (IOException e) {
            e.printStackTrace();
        }
        }
    
        static void zipFolder(String _path) throws IOException
        {
            Path path = Paths.get(_path);
            String target = "/home/hadoop/mytest/test.zip";
            //String target = path.getParent() +"/" + path.getFileName() +".zip";
            /*
             System.out.println(path.getFileName());
             System.out.println(path.getRoot());
             System.out.println(path.getParent());System.out.println(target);   
            */
             ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(target));
             zipFile(zo,path,"");
             zo.close();
        }
        static void zipFile(ZipOutputStream zo,Path _path,String parentpath) throws IOException
        {
            File _file = _path.toFile();
            if(_file.isFile())
            {
                byte[] buff = new byte[1024];
                FileInputStream fi = new FileInputStream(_file);
                int len;
                zo.putNextEntry(new ZipEntry(parentpath +"/" + _file.getName()));
                while((len=fi.read(buff))>0)
                    zo.write(buff, 0, len);
                zo.closeEntry();
                fi.close();
            }
            if(_file.isDirectory())
            {
                if(_file.listFiles().length==0)
                {
                    zo.putNextEntry(new ZipEntry(parentpath.equals("")?_file.getName():parentpath + "/" + _file.getName() + "/"));
                }
                for(File __file : _file.listFiles())
                    zipFile(zo,__file.toPath(),parentpath.equals("")?_file.getName():parentpath+ "/" + _file.getName());
            }
        }
    
        static void unzip(String path,String target) throws IOException
        {
            File targetfolder = new File(target);
            ZipInputStream zi = new ZipInputStream(new FileInputStream(path));
            ZipEntry ze = null;
            FileOutputStream fo = null;
            byte[] buff = new byte[1024];
            int len;
            while((ze =  zi.getNextEntry())!=null)
            {
                File _file = new File(targetfolder,ze.getName());
                if(!_file.getParentFile().exists()) _file.getParentFile().mkdirs();
                if(ze.isDirectory())
                {
                    _file.mkdir();
                }
                else //file
                {
                    fo = new FileOutputStream(_file);
                    while((len=zi.read(buff))>0) fo.write(buff, 0, len);
                    fo.close();
                }
                zi.closeEntry();
            }        
            zi.close();
        }
    }
    
    Looking for a job working at Home about MSBI
  • 相关阅读:
    判断一个点是否在一个不规则多边形内算法
    vue-cli 3.0 安装和创建项目流程
    微信小程序分享朋友圈的实现思路与解决办法
    vue2.0中关于active-class
    Nginx服务启动脚本
    Linux系统优化
    URL检测脚本
    Mysql读写分离php脚本
    Memcahed服务异常监控脚本
    一致性哈希算法PHP测试片段
  • 原文地址:https://www.cnblogs.com/huaxiaoyao/p/4300502.html
Copyright © 2011-2022 走看看