java JDK中提供了处理zip文件的API,不过无法处理编码方式,不支持中文编码,apache提供了一组处理zip文件的API,包含在ant.jar,支持中文编码
,以下为本人用apache ant实现zip压缩及解压的代码:
package com.work.study.zip; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; /** * 使用apache API处理zip格式压缩 * @author wison Xu */ public class ZipTool { public static final String SYS_ENCODING = System.getProperty("file.encoding"); /** * 压缩文件 * @param files 文件数组 * @param zipFilePath zip文件位置 * @throws IOException */ public static void zip(String[] files, String zipFilePath) throws IOException { OutputStream out = null; OutputStream buffOut = null; ZipOutputStream zout = null; try { out = new FileOutputStream(zipFilePath); buffOut = new BufferedOutputStream(out); zout = new ZipOutputStream(buffOut); zout.setEncoding(SYS_ENCODING); //设置编码方式 for (int i = 0; i < files.length; i++) { File file = new File(files[i]); zip(file, "", zout); //递归压缩 } } finally { if (zout != null) zout.close(); if (buffOut != null) buffOut.close(); if (out != null) out.close(); } } private static void zip(File file, String parentEntryName, ZipOutputStream zout) throws IOException { String entryName = file.getName(); // 构建条目名称(名称中应包含父文件夹条目,如parent_dir/child_file) if (parentEntryName != null && !"".equals(parentEntryName.trim())) { entryName = parentEntryName + "/" + file.getName(); } if (file.isDirectory()) { File[] files = file.listFiles(); //如果是空文件夹,则直接往输出流放入文件夹条目 if (files.length == 0) { ZipEntry entry = new ZipEntry(entryName+"/"); zout.putNextEntry(entry); } //如果不是空文件夹,则递归其下的子文件 else { for (int i = 0; i < files.length; i++) { File subfile = files[i]; zip(subfile, entryName, zout); } } } else { ZipEntry entry = new ZipEntry(entryName); zip(file, entry, zout); } } private static void zip(File file, ZipEntry entry, ZipOutputStream zout) throws IOException { InputStream in = null; BufferedInputStream buffIn = null; try { in = new FileInputStream(file); buffIn = new BufferedInputStream(in, 1024); System.out.println("compressing:" + file.getPath()); zout.putNextEntry(entry); byte[] buff = new byte[1024]; int len = 0; while ((len = buffIn.read(buff)) != -1) { zout.write(buff, 0, len); } zout.closeEntry(); } catch (IOException e) { throw e; } finally { if(buffIn != null) buffIn.close(); if (in != null) in.close(); } } /** * 解压zip文件 * @param zipFilePath 带解压的zip文件 * @param targetDir 解压的目标文件路径 * @throws IOException */ public static void unzip(String zipFilePath, String targetDir) throws IOException { File zFile = new File(zipFilePath); ZipFile zipFile = new ZipFile(zFile, SYS_ENCODING); // 检查目标文件目录,如果不存在则创建 if(targetDir != null && !"".equals(targetDir.trim())) { File file = new File(targetDir); if (!file.exists()) { if(!file.mkdirs()) { throw new IOException(targetDir + " is illegal!"); } } else if (file.isFile()) { throw new IOException(targetDir + " is a file!"); } } else { targetDir = zFile.getParent(); } Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zipFile.getEntries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String entryName = entry.getName(); // 如果是文件夹条目,则创建该文件夹 if (entry.isDirectory()) { new File(targetDir + "/" + entryName).mkdirs(); } // 如果是文件条目,则解压该文件 else { String dir = entryName.substring(0, entryName.lastIndexOf("/")); new File(targetDir + "/" + dir).mkdirs(); BufferedOutputStream buffOut = null; OutputStream out = null; InputStream in = null; try { System.out.println("Extracting:" + entryName); out = new FileOutputStream(targetDir + File.separator + entryName); buffOut = new BufferedOutputStream(out, 1024); in = zipFile.getInputStream(entry); int len = 0; byte[] buff = new byte[1024]; while ((len = in.read(buff)) != -1) { buffOut.write(buff, 0, len); } } catch (IOException e) { System.out.println("/t>>>failed!"); throw e; } finally { if(buffOut != null) buffOut.close(); if(out != null) out.close(); if(in != null) in.close(); } } } } }
您还没有登录,请您登录后再发表评论
java 文件zip 压缩与解压 可以直接调用
NULL 博文链接:https://zhenyong.iteye.com/blog/1576148
java解压zip压缩文件,读取压缩文件中的内容并输出到输出文件中。
使用 Java 语言实现并封装的创建 ZIP 格式的压缩文件并解压到指定目录和解压 ZIP 文件到指定目录的工具类。
Java实现的带密码压缩与解压zip文件源码,可将多个文件使用不同的密码压进同一个zip文件;可以解压带密码的zip文件(store压缩模式)
java 操作Zip文件(压缩、解压、加密) zip4j-1.3.2.jar ant-1.10.6.jar
java实现的解压与压缩的类 其中有zip和rar类型的 还包括了所需的架包
对zip文件进行解压缩工具类,以及jar包
java版本的zip压缩和解压程序,命令行的,没有界面,支持文件夹压缩解压
JAVA实现目录及文件的zip压缩和解压
java压缩解压ZIP
关于Java实现文件的zip的解压缩 文本文档 虽然不多
java zip 压缩解压例子
包含了使用的jar包,以及一个Java类,实现了使用Java对多目录多层级的文件进行打包,以及对ZIP包进行解压缩的操作。
java控制指定路径下的文件夹压缩成zip格式。 java将zip压缩包解压成为文件夹。直接使用io流进行操作,不需借助其他jar包
zip文件压缩/解压源码,根据网上的部分代码进行重写,更方便使用
Java带密码压缩zip与解压文件源码源码整理
本文通过对数据压缩算法的简要介绍,然后以详细的示例演示了利用java.util.zip包实现数据的压缩与解压,并扩展到在网络传输方面如何应用java.util.zip包现数据压缩与解压
zip 压缩 文件 Java zip 压缩 文件 Java zip 压缩 文件 Java zip 压缩 文件 Java
java 压缩/解压 .zip/.rar/.tar 文件,大家一起讨论,学习 已实现zip压缩/解压,rar解压
相关推荐
java 文件zip 压缩与解压 可以直接调用
NULL 博文链接:https://zhenyong.iteye.com/blog/1576148
java解压zip压缩文件,读取压缩文件中的内容并输出到输出文件中。
使用 Java 语言实现并封装的创建 ZIP 格式的压缩文件并解压到指定目录和解压 ZIP 文件到指定目录的工具类。
Java实现的带密码压缩与解压zip文件源码,可将多个文件使用不同的密码压进同一个zip文件;可以解压带密码的zip文件(store压缩模式)
java 操作Zip文件(压缩、解压、加密) zip4j-1.3.2.jar ant-1.10.6.jar
java实现的解压与压缩的类 其中有zip和rar类型的 还包括了所需的架包
对zip文件进行解压缩工具类,以及jar包
java版本的zip压缩和解压程序,命令行的,没有界面,支持文件夹压缩解压
JAVA实现目录及文件的zip压缩和解压
java压缩解压ZIP
关于Java实现文件的zip的解压缩 文本文档 虽然不多
java zip 压缩解压例子
包含了使用的jar包,以及一个Java类,实现了使用Java对多目录多层级的文件进行打包,以及对ZIP包进行解压缩的操作。
java控制指定路径下的文件夹压缩成zip格式。 java将zip压缩包解压成为文件夹。直接使用io流进行操作,不需借助其他jar包
zip文件压缩/解压源码,根据网上的部分代码进行重写,更方便使用
Java带密码压缩zip与解压文件源码源码整理
本文通过对数据压缩算法的简要介绍,然后以详细的示例演示了利用java.util.zip包实现数据的压缩与解压,并扩展到在网络传输方面如何应用java.util.zip包现数据压缩与解压
zip 压缩 文件 Java zip 压缩 文件 Java zip 压缩 文件 Java zip 压缩 文件 Java
java 压缩/解压 .zip/.rar/.tar 文件,大家一起讨论,学习 已实现zip压缩/解压,rar解压