用ZipInputStream和ZipOutputStream实现文件及文件夹的压缩解压

主要是运用ZipInputStream和ZipOutputStream实现

当中要注意,在文件解压时,ZipEntry中的名字是相对路径

File的方法createNewFile()只能在存在的目录下创建文件,所以在有多层目录时,先要mkdirs来创建目录,然后创建文件

代码如下

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


public class Main  
{
    private void zip(ZipOutputStream out, File f, String base) throws Exception
    {
        if (f.isDirectory()) {
            File[] files = f.listFiles();
            base = (base.length() == 0 ? "" : base + "/");
            for (int i = 0; i < files.length; i++) {
                zip(out, files[i], base + files[i].getName());
            }
        } else {
            out.putNextEntry(new ZipEntry(base));
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
            int c;
            
            while ((c = in.read()) != -1) {
                out.write(c);
            }
            in.close();
        }
    }
    
    private void zip(File inputFileName, String zipFileName) throws Exception
    {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        zip(out, inputFileName, "");
        out.close();
    }
    
    //压缩文件,inputFileName表示要压缩的文件(可以为目录),zipFileName表示压缩后的zip文件
    public void zip(String inputFileName, String zipFileName) throws Exception
    {
        zip(new File(inputFileName), zipFileName);
    }
    
    //解压,zipFileName表示待解压的zip文件,unzipDir表示解压后文件存放目录
    public void unzip(String zipFileName, String unzipDir) throws Exception
    {
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
        ZipEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            
            String fileName = entry.getName();
           
            //有层级结构,就先创建目录
            String tmp;
            int index = fileName.lastIndexOf('/');
            if (index != -1) {
                tmp = fileName.substring(0, index);
                tmp = unzipDir + "/" + tmp;
                File f = new File(tmp);
                f.mkdirs();
            }
            
            //创建文件
            fileName = unzipDir + "/" + fileName;
            File file = new File(fileName);
            file.createNewFile();
            
            FileOutputStream out = new FileOutputStream(file);
            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
            out.close();
        }
        in.close();
    }
    
    public static void main(String[] args)
    {
        try {
            
            String inputFileName = "F:\\test";
            String zipFileName = "F:\\test.zip";
            Main test = new Main();
           
            test.unzip(zipFileName, inputFileName);

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


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以使用Java的ZipInputStream类来解压文件,并且在解压后修改解压文件夹名称。下面是示例代码: ```java import java.io.*; import java.util.zip.*; public class ZipExtractor { public static void extractAndRename(String zipFilePath, String destinationPath, String newFolderName) { try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) { ZipEntry entry = zipIn.getNextEntry(); byte[] buffer = new byte[1024]; while (entry != null) { String filePath = destinationPath + File.separator + entry.getName(); if (!entry.isDirectory()) { // Extract the file File outputFile = new File(filePath); new File(outputFile.getParent()).mkdirs(); try (FileOutputStream fos = new FileOutputStream(outputFile)) { int len; while ((len = zipIn.read(buffer)) > 0) { fos.write(buffer, 0, len); } } } else { // Rename the folder String folderPath = destinationPath + File.separator + entry.getName(); String newFolderPath = destinationPath + File.separator + newFolderName; File folder = new File(folderPath); File newFolder = new File(newFolderPath); folder.renameTo(newFolder); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String zipFilePath = "path/to/your/zip/file.zip"; String destinationPath = "path/to/your/destination/folder"; String newFolderName = "new_folder_name"; extractAndRename(zipFilePath, destinationPath, newFolderName); } } ``` 您需要将`zipFilePath`设置为要解压ZIP文件的路径,`destinationPath`设置为要解压到的目标文件夹路径,`newFolderName`设置为要修改的文件夹名称。然后运行`extractAndRename`方法即可完成解压和重命名操作。 请确保您已经在Java环境中设置了正确的文件路径和文件夹名称。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值