java实现文件压缩和解压

压缩代码

package com.swan.zib.compression;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 将文件夹下面的文件
 * 打包成zip压缩文件
 *
 * @author admin
 */
public final class FileZib {

    /**
     * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
     *
     * @param sourceFilePath :待压缩的文件路径
     * @param zipFilePath    :压缩后存放路径
     * @param fileName       :压缩后文件的名称
     * @return
     */
    public static boolean zip(String sourceFilePath, String zipFilePath, String fileName) {
        boolean flag = false;
        File sourceFile = new File(sourceFilePath);
        FileInputStream fis = null;

        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;

        //判断源文件是否存在
        if (sourceFile.exists() == false) {
            System.out.println("待解压文件不存在");

        } else {
            File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
            //判断文件是否存在或者同名

            if (zipFile.exists()) {
                System.out.println("此文件已存在");
            } else {
                File[] sourceFiles = sourceFile.listFiles();
                //判断带压缩文件是否存在
                if (sourceFile == null || sourceFiles.length < 1) {
                    System.out.println("待压缩文件目录不存在任何文件");
                } else {
                    try {
                        fos = new FileOutputStream(zipFile);
                        zos = new ZipOutputStream(new BufferedOutputStream(fos));   //存入缓存

                        byte[] bytes = new byte[1024 * 10];
                        for (int i = 0; i < sourceFiles.length; i++) {
                            //创建Zip实体,添加压缩包
                            ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
                            zos.putNextEntry(zipEntry);

                            //读取待解压文件并写进压缩包里
                            fis = new FileInputStream(sourceFiles[i]);

                            //读取缓冲区文件
                            bis = new BufferedInputStream(fis, 1024 * 10);

                            int read = 0;
                            while ((read = bis.read(bytes, 0, 1024 * 10)) != -1) {
                                //将读取到的byte数组写入输出流
                                System.out.println(fileName + "正在压缩");
                                zos.write(bytes, 0, read);
                            }

                        }

                        flag = true;

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (bis != null) {
                            try {
                                bis.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (zos != null) {
                            try {
                                zos.close();
                                zos.flush();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
        return flag;
    }


    //主方法
    public static void main(String[] args) {
        String sourceFilePath = "/Users/dllo/Desktop/DJ0703-简历1";     //需要压缩的文件目录
        String zipFilePath = "/Users/dllo/Desktop/临时文件";            //压缩文件后放到的指定目录
        String fileName = "小柯压缩4";                                  //压缩文件名称
        boolean flag = FileZib.zip(sourceFilePath, zipFilePath, fileName);
        if (flag) {
            System.out.println("文件压缩成功");
        } else {
            System.out.println("文件压缩失败");
        }
    }
}

解压代码

package com.swan.zib.compression;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * Created by dllo on 18/1/4.
 */
public class ReFileZib {

    /**
     * 解压文件
     *
     * @param
     * @param
     * @return
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        long startTime = System.currentTimeMillis();
        try {
            ZipInputStream Zin = new ZipInputStream(new FileInputStream(
                    "/Users/dllo/Desktop/临时文件/小柯压缩3.zip"));//输入源zip路径
            BufferedInputStream Bin = new BufferedInputStream(Zin);
            String Parent = "/Users/dllo/Desktop/临时文件"; //输出路径(文件夹目录)
            File Fout = null;
            ZipEntry entry;
            try {
                while ((entry = Zin.getNextEntry()) != null && !entry.isDirectory()) {
                    Fout = new File(Parent, entry.getName());
                    if (!Fout.exists()) {
                        (new File(Fout.getParent())).mkdirs();
                    }
                    FileOutputStream out = new FileOutputStream(Fout);
                    BufferedOutputStream Bout = new BufferedOutputStream(out);
                    int b;
                    while ((b = Bin.read()) != -1) {
                        Bout.write(b);
                    }
                    Bout.close();
                    out.close();
                    System.out.println(Fout + "解压成功");
                }
                Bin.close();
                Zin.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("耗费时间: " + (endTime - startTime) + " ms");
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值