欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java.util.zip

程序员文章站 2022-05-15 17:41:46
...

(一)压缩单个文件

 public static void main(String[] args) {
        //压缩单个文件
        //要压缩的文件路径
        String filepath="D:\\markavip\\测试.txt";
        //文件压缩之后的路径
        String zippath="D:\\markavip\\test.zip";
        zipFile(filepath,zippath);
    }

    public static void zipFile(String filepath,String zippath){
        InputStream input=null;
        //压缩
        ZipOutputStream zipOut=null;
        try{
            File file=new File(filepath);
            File zipFile=new File(zippath);

            input=new FileInputStream(file);
            zipOut=new ZipOutputStream(new FileOutputStream(zipFile));
            zipOut.putNextEntry(new ZipEntry(file.getName()));

            int tmp=0;
            byte[] buffer=new byte[1024];
            while((tmp=input.read(buffer,0,1024))!=-1){
                zipOut.write(buffer,0,tmp);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try{
                input.close();
                zipOut.close();

            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
    }

(二)、压缩一个文件夹(包含多个文件)

 public static void main(String[] args) {
        //文件夹路径
        String filepath="D:\\markavip\\";
        //压缩后的文件夹路径
        String zippath="D:\\markTest.zip";
        zipDirFile(filepath,zippath);
    }

    public static void zipDirFile(String filepath,String zippath){
        try{
            File file=new File(filepath);
            File zipFile=new File(zippath);

            InputStream input=null;
            ZipOutputStream zipOut=new ZipOutputStream(new FileOutputStream(zipFile));

            if(file.isDirectory()){
                File[]  files=file.listFiles();
                for(int i=0;i<files.length;i++){
                    input=new FileInputStream(files[i]);
                    zipOut.putNextEntry(new ZipEntry(file.getName()+File.separator+files[i].getName()));
                    int tmp=0;
                    byte[] buf=new byte[1024];
                    while((tmp=input.read(buf,0,1024))!=-1){
                        zipOut.write(buf,0,tmp);
                    }
                    input.close();
                }
            }else{
                //调用单个压缩文件
                    ZipOneFile.zipFile(filepath,zippath);
            }
            zipOut.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

(三)解压单个文件

public static void main(String[] args) {
        //压缩的文件路径
        String zippath="D:\\markavip\\test.zip";
        //解压之后的文件路径
        String outfilepath="D:\\markavip\\one.txt";
        //压缩文件中的文件名
        String filename="测试.txt";
        zipContraFile(zippath,outfilepath,filename);
    }
    public static void  zipContraFile(String zippath,String outfilepath,String filename){
        try{
            File file=new File(zippath);
            File outFile=new File(outfilepath);
            ZipFile zipFile=new ZipFile(file);
            ZipEntry entry=zipFile.getEntry(filename);
            InputStream input=zipFile.getInputStream(entry);
            OutputStream output=new FileOutputStream(outFile);
            int tmp=0;
            byte[] buf=new byte[1024];
            while((tmp=input.read(buf,0,1024))!=-1){
                output.write(buf,0,tmp);
            }
            input.close();
            output.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }
    }

(四)、解压一个文件夹(内含多个文件)

 public static void main(String[] args) {
        //压缩文件路径
        String zippath="D:\\markTest.zip";
        //解压后的路径
        String outfilepath="D:\\YOHO";
        zipContraMultiFile(zippath,outfilepath);
    }

    public static void zipContraMultiFile(String zippath,String outfilepath ){
        ZipInputStream zipInput=null;
        ZipFile zipFile=null;
        InputStream input=null;
        OutputStream output=null;
        try{
            File file=new File(zippath);
            File outFile=null;

            zipFile=new ZipFile(file);
            zipInput=new ZipInputStream(new FileInputStream(file));

            ZipEntry entry=null;
            while ((entry=zipInput.getNextEntry())!=null){
                outFile=new File(outfilepath+File.separator+entry.getName());
                if(!outFile.getParentFile().exists()){
                    outFile.getParentFile().mkdir();
                }
                if(!outFile.exists()){
                    outFile.createNewFile();
                }
                input=zipFile.getInputStream(entry);
                output=new FileOutputStream(outFile);
                int tmp=0;
                byte[] buf=new byte[1024];
                while((tmp=input.read(buf,0,1024))!=-1){
                    output.write(buf,0,tmp);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                input.close();
                output.close();
                zipInput.close();
                zipFile.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

参考文章:
http://www.2cto.com/kf/201412/363662.html
http://www.cnblogs.com/chiangfai/p/5888141.html
http://blog.csdn.net/u011423071/article/details/52043702

相关标签: 压缩

上一篇: lucene-索引RTF文档

下一篇: gulp