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
  • 相关阅读:
    刚开始用springboot踩的好多坑!!!
    AngularJS学习(一)
    linux上的第一个c语言程序
    设计模式——6大设计原则
    C# List的深复制
    C# XML 操作
    C#多线程学习
    实现树形结构
    观察者模式
    python3.3 MD5
  • 原文地址:https://www.cnblogs.com/huaxiaoyao/p/4300502.html
Copyright © 2011-2022 走看看