使用jdk自带的zip工具类Java.util.zip.ZipEntry,java.util.zip.ZipFile,java.util.zip.ZipInputStream,java.util.zip.ZipOutputStream 进行zip压缩时,没法解决文件名中文乱码问题
这里使用apache 旗下的commons-compress 库,官网是:http://commons.apache.org/proper/commons-compress/download_compress.cgi
我使用maven 进行构建,pom配置如下:
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-compress</artifactId>
- <version>1.5</version>
- </dependency>
commons-compress 解决了文件名中文乱码问题,参考:http://www.cnblogs.com/un4sure/archive/2011/09/27/2193298.html
范例:
(1)压缩单个文件:
压缩D:\\Temp\\a\\password_密码.xls,压缩后的zip文件是d:\\Temp\\a\\a\\b\\c.zip
- package com.jn.test;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import org.apache.commons.compress.archivers.ArchiveException;
- import org.apache.commons.compress.archivers.ArchiveOutputStream;
- import org.apache.commons.compress.archivers.ArchiveStreamFactory;
- import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
- import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
- import org.junit.Test;
- import com.common.util.SystemUtil;
- import com.io.hw.file.util.FileUtils;
- public class ZIPTest {
- @Test
- public void test_01() {
- try {
- FileOutputStream fou = new FileOutputStream("d:\\Temp\\a\\a\\b\\c.zip");
- ArchiveOutputStream archOuts = new ArchiveStreamFactory()
- .createArchiveOutputStream(ArchiveStreamFactory.ZIP, fou);
- if(archOuts instanceof ZipArchiveOutputStream){
- ZipArchiveOutputStream zipOut=(ZipArchiveOutputStream)archOuts;
- String file="D:\\Temp\\a\\password_密码.xls";
- ZipArchiveEntry zipEntry=new ZipArchiveEntry(new File(file),SystemUtil.getFileSimpleName(file));
- zipOut.putArchiveEntry(zipEntry);
- zipOut.write(FileUtils.readBytes4file(file));
- zipOut.closeArchiveEntry();
- zipOut.flush();
- zipOut.finish();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (ArchiveException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
运行之后会生成文件d:\\Temp\\a\\a\\b\\c.zip:
(1)压缩多个文件:
- @Test
- public void test_02() {
- try {
- FileOutputStream fou = new FileOutputStream(
- "d:\\Temp\\a\\a\\b\\c.zip");
- ArchiveOutputStream archOuts = new ArchiveStreamFactory()
- .createArchiveOutputStream(ArchiveStreamFactory.ZIP, fou);
- if (archOuts instanceof ZipArchiveOutputStream) {
- ZipArchiveOutputStream zipOut = (ZipArchiveOutputStream) archOuts;
- String file01 = "D:\\Temp\\a\\password_密码.xls";
- ZipArchiveEntry zipEntry = new ZipArchiveEntry(
- new File(file01), SystemUtil.getFileSimpleName(file01));
- zipOut.putArchiveEntry(zipEntry);
- zipOut.write(FileUtils.readBytes4file(file01));
- String file02 = "D:\\Temp\\a\\ccc.jar";
- ZipArchiveEntry zipEntry2 = new ZipArchiveEntry(
- new File(file01), SystemUtil.getFileSimpleName(file02));
- zipOut.putArchiveEntry(zipEntry2);
- zipOut.write(FileUtils.readBytes4file(file02));
- zipOut.closeArchiveEntry();
- zipOut.flush();
- zipOut.finish();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (ArchiveException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
压缩后的zip文件:
时间: 2024-11-09 00:17:09