Java压缩解压缩zip文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ZipUtil {

    public static void main(String[] args) {
        List<File> list = new ArrayList<File>();
        list.add(new File("d:/无标题.JPG"));
        list.add(new File("d:/各个模块截止时间.txt"));
        list.add(new File("d:/FF276"));
       
        zip(list.toArray(new File[0]), new File("d:/test.zip"));
    }
   
    public static void unzip(String unzipFile, String savePath) {
        decompression(unzipFile, savePath);
    }
   
    public static void zip(File[] srcFiles, File destFile) {
        compression(srcFiles, destFile);
    }
   
    public static void compression(File[] srcFiles, File destFile) {
        org.apache.tools.zip.ZipOutputStream zipOut = null;
        try {
            zipOut = new org.apache.tools.zip.ZipOutputStream(new FileOutputStream(destFile));

            zipOut.setEncoding("GBK");
            System.out.println("encode === " + zipOut.getEncoding());
            for (File file : srcFiles) {
                process(zipOut, file, "");
            }
       
            zipOut.close();
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            if (zipOut != null) try {zipOut.close();} catch(Exception e) {}
        }
    }
   
    private static void process(org.apache.tools.zip.ZipOutputStream zipOut,
            File src, String path) throws IOException {
        org.apache.tools.zip.ZipEntry entry = null;
        if (src.isDirectory()) {
            path = path + src.getName() + File.separator;
            for (File file : src.listFiles()) {
                process(zipOut, file, path);
            }
        } else {
            entry = new org.apache.tools.zip.ZipEntry(path + src.getName());
            zipOut.putNextEntry(entry);
            FileInputStream fis = new FileInputStream(src);
            byte[] buf = new byte[4 * 1024];
            int len = 0;
            while ((len = fis.read(buf)) > 0) {
                zipOut.write(buf, 0, len);
            }
            fis.close();
        }
    }

    public static void decompression(String fileName, String savePath) {
        org.apache.tools.zip.ZipFile zipFile = null;
        org.apache.tools.zip.ZipEntry entry = null;
        try {
            File sourceFile = new File(fileName);
            zipFile = new org.apache.tools.zip.ZipFile(sourceFile, "GBK");
            byte[] buffer = new byte[4 * 1024];

            java.util.Enumeration<org.apache.tools.zip.ZipEntry> e = zipFile.getEntries();
            while (e.hasMoreElements()) {
                entry = e.nextElement();
                String entryName = entry.getName();
                if (entry.isDirectory()) {//目录
                    System.out.println("目录: " + entryName);
                    File directory = new File(savePath, entryName);
                    directory.mkdirs();
                } else {//文件
                    System.out.println("文件: " + entryName);
                    InputStream in = null;
                    FileOutputStream fos = null;
                    try {
                        in = zipFile.getInputStream(entry);
                        File file = new File(savePath, entryName);
                        File parentFile = file.getParentFile();
                        parentFile.mkdirs();
                        System.out.println("AbsolutePath = " + parentFile.getAbsolutePath());
                        fos = new FileOutputStream(file); 

                        int length = 0;
                        while ((length = in.read(buffer)) > 0) {
                            fos.write(buffer, 0, length);
                        }
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    } finally {
                        if (in != null) try {in.close();} catch(Exception ex) {}
                        if (fos != null) try {fos.close();} catch(Exception ex) {}
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipFile != null) try {zipFile.close();} catch(Exception e) {}
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
解压缩zip文件中含有中文名的文件时,可能会出现乱码或者解压失败的问题。这是因为在压缩文件时,文件名使用了默认编码格式,而在解压缩时,解压软件使用了不同的编码格式,导致文件名解析错误。 为了解决这个问题,可以通过指定解压缩文件名的编码格式来解决。以下是一个Java程序示例,用于解压缩zip文件并处理中文文件名的编码问题: ```java import java.io.*; import java.util.*; import java.util.zip.*; public class ZipUtils { public static void unzip(File zipFile, File destDir, String charset) throws IOException { if (!destDir.exists()) { destDir.mkdirs(); } ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFile), Charset.forName(charset)); ZipEntry entry; byte[] buffer = new byte[1024]; while ((entry = zipIn.getNextEntry()) != null) { String fileName = entry.getName(); if (entry.isDirectory()) { File subDir = new File(destDir, fileName); subDir.mkdirs(); continue; } File file = new File(destDir, fileName); File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } OutputStream out = new FileOutputStream(file); int len; while ((len = zipIn.read(buffer)) > 0) { out.write(buffer, 0, len); } out.close(); } zipIn.close(); } } ``` 在调用此方法时,可以指定解压缩文件名的编码格式,例如: ```java ZipUtils.unzip(new File("sun.zip"), new File("destDir"), "GBK"); ``` 这里的编码格式使用了GBK,根据实际情况可以修改为其他编码格式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值