利用Java zip进行对文件的压缩和解压

x33g5p2x  于2022-05-28 转载在 Java  
字(5.8k)|赞(0)|评价(0)|浏览(465)

利用Java JDK自带 进行对文件的压缩和解压

实现一个文件的zip压缩,过程可以简单地表示为:

ZipEntry:表示 ZIP 文件条目

构造方法:

  • public ZipEntry(String name) 可以用文件的相对路径来构造ZipEntry对象

ZipOutputStream: ZIP 文件格式写入文件实现输出流过滤器用于文件的解压

  • public void write(byte[] b,int off,int len) throws IOException
    将字节数组写入当前 ZIP 条目数据。在写入所有字节之前,此方法将阻塞。
  • public void setLevel(int level)
    为后续的 DEFLATED 条目设置压缩级别。默认设置是 DEFAULT_COMPRESSION。
  • public void close() throws IOException
    关闭 ZIP 输出流和正在过滤的流。
  • public void putNextEntry(ZipEntry e) throws IOException
    开始写入新的 ZIP 文件条目并将流定位到条目数据的开始处。如果仍处于活动状态,则关闭当前条目。如果没有为条目指定压缩方法,则使用默认的压缩方法;如果没有为条目设置修改时间,则使用当前时间。
  • public void closeEntry()throws IOException
    关闭当前 ZIP 条目并定位流以写入下一个条目。

ZipInputStream :读取 ZIP 文件格式的文件实现输入流过滤器(解压文件)

  • public ZipEntry getNextEntry()throws IOException
    读取下一个 ZIP 文件条目并将流定位到该条目数据的开始处。
  • public int read(byte[] b,int off,int len)throws IOException
    从当前 ZIP 条目读入字节数组。如果 len 不为零,则在某些输入可用之前,此方法将处于阻塞状态;否则,不读取字节并且返回 0

获取目录的所有子目录和文件

/**
     * 得到源文件路径的所有文件
     * @param dirFile 压缩源文件路径
     * */
    public static List<File> getAllFile(File dirFile){

        List<File> fileList=new ArrayList<>();

        File[] files= dirFile.listFiles();
        for(File file:files){//文件
            if(file.isFile()){
                fileList.add(file);
                System.out.println("add file:"+file.getName());
            }else {//目录
                if(file.listFiles().length!=0){//非空目录
                    fileList.addAll(getAllFile(file));//把递归文件加到fileList中
                }else {//空目录
                    fileList.add(file);
                    System.out.println("add empty dir:"+file.getName());
                }
            }
        }
        return fileList;
    }

获取文件的相对路径:

/**
     * 获取相对路径
     * @param dirPath 源文件路径
     * @param file 准备压缩的单个文件
     * */
    public static String getRelativePath(String dirPath,File file){
        File dirFile=new File(dirPath);
        String relativePath=file.getName();

        while (true){
            file=file.getParentFile();
            if(file==null) break;
            if(file.equals(dirFile)){
                break;
            }
            else {
                relativePath=file.getName()+"/"+relativePath;
            }
        }
        return relativePath;
    }

完整项目的代码:

/**
 * java.util.zip压缩/解压文件
 * */
public class ZipUtil {
 
    /** 缓冲器大小 */
    private static final int BUFFER = 512;
 
    /**压缩得到的文件的后缀名*/
    private static final String SUFFIX=".zip";
 
    /**
     * 得到源文件路径的所有文件
     * @param dirFile 压缩源文件路径
     * */
    public static List<File>  getAllFile(File dirFile){
 
        List<File> fileList=new ArrayList<>();
 
        File[] files= dirFile.listFiles();
        for(File file:files){//文件
            if(file.isFile()){
                fileList.add(file);
                System.out.println("add file:"+file.getName());
            }else {//目录
               if(file.listFiles().length!=0){//非空目录
                  fileList.addAll(getAllFile(file));//把递归文件加到fileList中
               }else {//空目录
                   fileList.add(file);
                   System.out.println("add empty dir:"+file.getName());
               }
            }
        }
        return fileList;
    }
 
    /**
     * 获取相对路径
     * @param dirPath 源文件路径
     * @param file 准备压缩的单个文件
     * */
    public static String getRelativePath(String dirPath,File file){
        File dirFile=new File(dirPath);
        String relativePath=file.getName();
 
        while (true){
            file=file.getParentFile();
            if(file==null) break;
            if(file.equals(dirFile)){
                break;
            }
            else {
                relativePath=file.getName()+"/"+relativePath;
            }
        }
        return relativePath;
    }
 
 
 
    /**
     *@param destPath 解压目标路径
     *@param fileName 解压文件的相对路径
     * */
    public static File createFile(String destPath, String fileName){
 
        String[] dirs = fileName.split("/");//将文件名的各级目录分解
        File file = new File(destPath);
 
        if (dirs.length > 1) {//文件有上级目录
            for (int i = 0; i < dirs.length - 1; i++) {
                file = new File(file, dirs[i]);//依次创建文件对象知道文件的上一级目录
            }
 
            if (!file.exists()) {
                file.mkdirs();//文件对应目录若不存在,则创建
                try {
                    System.out.println("mkdirs: " + file.getCanonicalPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
            file = new File(file, dirs[dirs.length - 1]);//创建文件
 
            return file;
        } else {
            if (!file.exists()) {//若目标路径的目录不存在,则创建
                file.mkdirs();
                try {
                    System.out.println("mkdirs: " + file.getCanonicalPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
            file = new File(file, dirs[0]);//创建文件
 
            return file;
        }
 
    }
 
    /**
     * 没有指定压缩目标路径进行压缩,用默认的路径进行压缩
     * @param dirPath 压缩源文件路径
     * */
    public static void compress(String dirPath){
 
        int firstIndex= dirPath.indexOf("/");
        int lastIndex= dirPath.lastIndexOf("/");
        String zipFileName=dirPath.substring(0,firstIndex+1)+dirPath.substring(lastIndex+1);
        compress(dirPath,zipFileName);
    }
 
    /**
     * 压缩文件
     * @param dirPath 压缩源文件路径
     * @param zipFileName 压缩目标文件路径
     * */
    public static void compress(String dirPath,String zipFileName){
 
 
        zipFileName=zipFileName+SUFFIX;//添加文件的后缀名
 
        File dirFile=new File(dirPath);
        List<File> fileList= getAllFile(dirFile);
 
        byte[] buffer=new byte[BUFFER];
        ZipEntry zipEntry=null;
        int readLength=0;     //每次读取出来的长度
 
        try {
            // 对输出文件做CRC32校验
            CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(
                    zipFileName), new CRC32());
            ZipOutputStream zos = new ZipOutputStream(cos);
 
            for(File file:fileList){
 
                if(file.isFile()){   //若是文件,则压缩文件
 
                    zipEntry=new ZipEntry(getRelativePath(dirPath,file));  //
                    zipEntry.setSize(file.length());
                    zipEntry.setTime(file.lastModified());
                    zos.putNextEntry(zipEntry);
 
                    InputStream is=new BufferedInputStream(new FileInputStream(file));
 
                    while ((readLength=is.read(buffer,0,BUFFER))!=-1){
                        zos.write(buffer,0,readLength);
                    }
                    is.close();
                    System.out.println("file compress:"+file.getCanonicalPath());
                }else {     //若是空目录,则写入zip条目中
 
                    zipEntry=new ZipEntry(getRelativePath(dirPath,file));
                    zos.putNextEntry(zipEntry);
                    System.out.println("dir compress: " + file.getCanonicalPath()+"/");
                }
            }
            zos.close();  //最后得关闭流,不然压缩最后一个文件会出错
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 解压
     * */
    public static void decompress(String zipFileName,String destPath){
 
        try {
 
            zipFileName=zipFileName+SUFFIX;
            ZipInputStream zis=new ZipInputStream(new FileInputStream(zipFileName));
            ZipEntry       zipEntry       = null;
            byte[]         buffer         = new byte[BUFFER];//缓冲器
            int            readLength     = 0;//每次读出来的长度
            while ((zipEntry=zis.getNextEntry())!=null){
                if(zipEntry.isDirectory()){//若是目录
                    File file=new File(destPath+"/"+zipEntry.getName());
                    if(!file.exists()){
                        file.mkdirs();
                        System.out.println("mkdirs:"+file.getCanonicalPath());
                        continue;
                    }
                }//若是文件
                    File file = createFile(destPath,zipEntry.getName());
                    System.out.println("file created: " + file.getCanonicalPath());
                    OutputStream os=new FileOutputStream(file);
                    while ((readLength=zis.read(buffer,0,BUFFER))!=-1){
                        os.write(buffer,0,readLength);
                    }
                os.close();
                System.out.println("file uncompressed: " + file.getCanonicalPath());
            }
 
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

相关文章