Java实现ZIP算法压缩和解压操作

2023-12-14 21:17:52

????????ZIP是一种流行的文件压缩格式,它可以将多个文件打包成一个文件,以减小文件大小并方便传输。ZIP文件可以在大多数操作系统和软件中解压缩,例如Windows、Mac和Linux系统上的许多文件管理器和解压缩工具。ZIP压缩可以使用许多不同的压缩算法,包括DEFLATE、LZMA和BZIP2。ZIP文件还支持文件加密和分卷压缩,可以方便地将大文件分成多个部分。

? ? ? ? 下面我们来一起实现一下ZIP压缩算法:

package com.xxx.utils;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * @author tobiasy
 * @date 2021/2/6
 */
public class ZipUtils {
    public static void main(String[] args) {
        File file = new File("F:\\test\\xxx");
        zip(file);

    }

    public static void unzip(File file) {
        try {
            ZipFile zipFile = new ZipFile(file, Charset.forName("gbk"));
            String targetContent = toCurrContent(file);
            FileCreator.createFile(targetContent, File::mkdir);
            Enumeration<? extends ZipEntry> entries;
            for (entries = zipFile.entries(); entries.hasMoreElements(); ) {
                ZipEntry zipEntry = entries.nextElement();
                File targetFile = new File(targetContent + "/" + zipEntry.getName());
                if (zipEntry.isDirectory()) {
                    FileCreator.createFile(targetContent, File::mkdir);
                } else {
                    FileCreator.newDirsFile(targetFile);
                    FileOutputStream os = new FileOutputStream(targetFile);
                    InputStream is = zipFile.getInputStream(zipEntry);
                    FileUtils.write(is, os);
                }

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

    public static void zip(File dir) {
        try {
            String zipPath = toCurrZip(dir);
            File targetFile = new File(zipPath);
            FileOutputStream os = new FileOutputStream(targetFile);
            ZipOutputStream zos = new ZipOutputStream(os);
            if (dir.isDirectory()) {
                addZipDir(dir, dir, zos);
            }
            zos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void addZipDir(File dir, File subFile, ZipOutputStream zos) {
        try {
            File[] files = subFile.listFiles();
            if (files == null) {
                return;
            }
            for (File file : files) {
                if (file.isFile()) {
                    String path = file.getPath();
                    String entryName = getZipEntryName(dir.getPath(), path);
                    System.out.println(entryName);
                    zos.putNextEntry(new ZipEntry(entryName));
                    FileInputStream is = new FileInputStream(file);
                    FileUtils.write(is, zos);
                    zos.closeEntry();
                    is.close();
                } else {
                    addZipDir(dir, file, zos);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String getZipEntryName(String basePath, String subFilePath) {
        String entryName = subFilePath.replace(basePath, "");
        String[] ss = new String[]{"/", "\\"};
        for (String s : ss) {
            if (entryName.startsWith(s)) {
                return entryName.substring(s.length());
            }
        }
        return entryName;
    }

    private static String toCurrContent(File file) {
        return file.getPath().replace(FileUtils.getFileSuffix(file.getPath()), "");
    }

    private static String toCurrZip(File dir) {
        return dir.getPath() + ".zip";
    }
}

文章来源:https://blog.csdn.net/m0_37649480/article/details/134931060
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。