传入一组文件,将文件列表打包为ZIP文件,传入ZIP文件将文件解压缩到一个指定文件夹,下面是这个实例的实现
使用到了ANT的工具类,所以要引入ant.jar文件到工程中
对于压缩建立一个工具类:
package com.xidian.commmon; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.zip.ZipOutputStream; import org.apache.tools.zip.ZipEntry; /** * ZIP操作工具类 * @说明 注意引入ant.jar文件 * @author cuisuqiang * @version 1.0 * @since */ public class Zip { /** * 压缩文件列表到某ZIP文件 * @param zipFilename 要压缩到的ZIP文件 * @param paths 文件列表,多参数 * @throws Exception */ public static void compress(String zipFilename, String... paths) throws Exception { compress(new FileOutputStream(zipFilename), paths); } /** * 压缩文件列表到输出流 * @param os 要压缩到的流 * @param paths 文件列表,多参数 * @throws Exception */ public static void compress(OutputStream os, String... paths) throws Exception { ZipOutputStream zos = new ZipOutputStream(os); for (String path : paths) { if (path.equals("")) continue; java.io.File file = new java.io.File(path); if (file.exists()) { if (file.isDirectory()) { zipDirectory(zos, file.getPath(), file.getName() + File.separator); } else { zipFile(zos, file.getPath(), ""); } } } zos.close(); } private static void zipDirectory(ZipOutputStream zos, String dirName, String basePath) throws Exception { File dir = new File(dirName); if (dir.exists()) { File files[] = dir.listFiles(); if (files.length > 0) { for (File file : files) { if (file.isDirectory()) { zipDirectory(zos, file.getPath(), basePath + file.getName().substring( file.getName().lastIndexOf( File.separator) + 1) + File.separator); } else zipFile(zos, file.getPath(), basePath); } } else { ZipEntry ze = new ZipEntry(basePath); zos.putNextEntry(ze); } } } private static void zipFile(ZipOutputStream zos, String filename, String basePath) throws Exception { File file = new File(filename); if (file.exists()) { FileInputStream fis = new FileInputStream(filename); ZipEntry ze = new ZipEntry(basePath + file.getName()); zos.putNextEntry(ze); byte[] buffer = new byte[8192]; int count = 0; while ((count = fis.read(buffer)) > 0) { zos.write(buffer, 0, count); } fis.close(); } } }
String... paths 是传入的文件列表,输出流可以穿文件流也可以传文件路径名称
压缩一组文件:
package com.xidian.test; import java.io.FileOutputStream; import com.xidian.commmon.Zip; /** * 压缩文件 * @说明 * @author cuisuqiang * @version 1.0 * @since */ public class TestCompress { public static void main(String[] args) { // 要压缩的文件列表 String path01 = "C:\\a"; String path02 = "C:\\test.txt"; try { FileOutputStream os = new FileOutputStream("C:\\a.zip"); // 输出的ZIP文件流 Zip.compress(os, path01, path02); } catch (Exception e) { e.printStackTrace(); } } }
由于已经写了工具类,调用看起来非常清晰
解压这个压缩包:
package com.xidian.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** * 解压缩测试 * @说明 * @author cuisuqiang * @version 1.0 * @since */ public class EctractZip { @SuppressWarnings( { "unchecked", "static-access" }) public static void main(String[] args) { EctractZip z = new EctractZip(); ArrayList<String> a = z.Ectract("C:\\a.zip", "C:\\tmp\\"); // 返回解压缩出来的文件列表 for(String s : a){ System.out.println(s); } } /** * 解压缩 * @param sZipPathFile 要解压的文件 * @param sDestPath 解压到某文件夹 * @return */ @SuppressWarnings("unchecked") public static ArrayList Ectract(String sZipPathFile, String sDestPath) { ArrayList<String> allFileName = new ArrayList<String>(); try { // 先指定压缩档的位置和档名,建立FileInputStream对象 FileInputStream fins = new FileInputStream(sZipPathFile); // 将fins传入ZipInputStream中 ZipInputStream zins = new ZipInputStream(fins); ZipEntry ze = null; byte[] ch = new byte[256]; while ((ze = zins.getNextEntry()) != null) { File zfile = new File(sDestPath + ze.getName()); File fpath = new File(zfile.getParentFile().getPath()); if (ze.isDirectory()) { if (!zfile.exists()) zfile.mkdirs(); zins.closeEntry(); } else { if (!fpath.exists()) fpath.mkdirs(); FileOutputStream fouts = new FileOutputStream(zfile); int i; allFileName.add(zfile.getAbsolutePath()); while ((i = zins.read(ch)) != -1) fouts.write(ch, 0, i); zins.closeEntry(); fouts.close(); } } fins.close(); zins.close(); } catch (Exception e) { System.err.println("Extract error:" + e.getMessage()); } return allFileName; } }
请您到ITEYE网站看原创,谢谢!
http://cuisuqiang.iteye.com/ !
自建博客地址:http://www.javacui.com/ ,内容与ITEYE同步!
解压其实非常简单,因为JDK 中有一个ZipInputStream对象,只需要使用他为我们服务即可!
相关推荐
java 解压缩zip文件 java 解压缩zip文件 java 解压缩zip文件
JAVA文件压缩与解压缩实践,java解压缩zip文件,Java源码
java 中压缩ZIP格式源码,仅供参考。
java语言操作解压缩文件。 /** * 数据压缩 * * @param data * @return * @throws Exception */ public static byte[] compress(byte[] data) throws Exception { ByteArrayInputStream bais = new ...
JAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩...
对zip文件进行解压缩工具类,以及jar包
java zip文件的压缩与解压缩,支持中文 使用apache的工具包可设置字符编码
java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文).zip java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文).zip java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文)....
Java解压缩文件,并以ZIP格式压缩文件,主要是使用java.util.zip 包中的类来实现解压、压缩文件功能,如果你对这个类并不太熟悉,你正好可以参考一下这个类是如何用的。
zip解压缩文件文件夹都可以,代码清晰,注释多。非常容易看懂
java解压缩 rar 以及 zip 格式文件
JAVA实现解压缩ZIP格式的压缩文件(含中文名文件)的完整DEMO,含用到的JAR包。
JAVA文件压缩与解压缩实践(源代码+论文)
java实现的解压与压缩的类 其中有zip和rar类型的 还包括了所需的架包
java 实现压缩与解压缩ZIP .docjava 实现压缩与解压缩ZIP .docjava 实现压缩与解压缩ZIP .docjava 实现压缩与解压缩ZIP .doc
使用java实现解压缩zip文件,有详细的注释。
zip格式是一种免费的压缩文件格式,我们可以通过java类库编写函数来解压缩它,实现winwar这类软件的功能。
poi.zip java读取excel文件,其中bin压缩文件中含有jar包,eclipse导入时可直接使用;src压缩文件含有java源代码,需要阅读源码的童鞋可以下载使用