Java---压缩与解压缩

压缩

package zip;

/**
 * @ClassName---ZipUtil
 * @Description TODO
 * @Author MF
 * @Data 2023/4/10 21:21
 * @Version 1.0
 **/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
    public static void main(String[] args) throws Exception {
        File file = new File("./demo.txt");
        String name = "demo.zip";
        zipFile(file,name);
    }


    public static void zipFile(File sourceFile, String targetFileName) throws Exception {
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        try {
            fos = new FileOutputStream(targetFileName);
            zos = new ZipOutputStream(fos);
            ZipEntry entry = new ZipEntry(sourceFile.getName());
            zos.putNextEntry(entry);
            FileInputStream fis = new FileInputStream(sourceFile);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
            zos.closeEntry();
            fis.close();
        } finally {
            if (zos != null) {
                zos.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }

}

解压缩文件:

import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class UnzipUtil {

    public static void unzipFile(String sourceFileName, String targetDirectory) throws Exception {
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(sourceFileName);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            byte[] buffer = new byte[1024];
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (entry.isDirectory()) {
                    continue;
                }
                File entryFile = new File(targetDirectory, entry.getName());
                if (!entryFile.getParentFile().exists()) {
                    entryFile.getParentFile().mkdirs();
                }
                FileOutputStream fos = new FileOutputStream(entryFile);
                try {
                    int len;
                    while ((len = zipFile.getInputStream(entry).read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                } finally {
                    fos.close();
                }
            }
        } finally {
            if (zipFile != null) {
                zipFile.close();
            }
        }
    }

}

使用时,可以通过调用ZipUtil.zipFile方法来进行压缩操作,也可以通过调用UnzipUtil.unzipFile方法来进行解压缩操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值