码迷,mamicode.com
首页 > 编程语言 > 详细

Java zip and unzip demo

时间:2015-02-26 08:39:06      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

目录结构如下:

技术分享

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

 

Java zip and unzip demo

标签:

原文地址:http://www.cnblogs.com/huaxiaoyao/p/4300502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!