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();
    }
}

 

posted on 2015-02-26 08:05  tneduts  阅读(1727)  评论(0编辑  收藏  举报

导航