Java 压缩zip格式文件
来源:互联网 发布:模拟退火算法例题 编辑:程序博客网 时间:2024/05/17 06:03
三个主要的类:
- import java.util.zip.ZipFile;//打开zip格式文件。
- import java.util.zip.ZipEntry; //标记被压缩文件的属性,主要是文件名和路径。
- import java.util.zip.ZipOutputStream;//zip格式输出流。
压缩文件夹时,需要挨个结点,递归压缩文件夹中所有的文件。
public static void zipFile(String filePath, String zipPath) { try { File inFile = new File(filePath); File outFile = new File(zipPath); FileOutputStream fos = new FileOutputStream(outFile); ZipOutputStream zos = new ZipOutputStream(fos); zipFile(filePath, inFile, zos); zos.close(); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 压缩文件夹 * @author flueky zkf@yitong.com.cn * @date 2016年4月13日 下午7:47:11 * @param folderPath * @param zipPath */ public static void zipFolder(String folderPath, String zipPath) { try { File inFile = new File(folderPath); File outFile = new File(zipPath); FileOutputStream fos = new FileOutputStream(outFile); ZipOutputStream zos = new ZipOutputStream(fos); zipFile(folderPath, inFile, zos); zos.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 递归压缩文件 * @author flueky zkf@yitong.com.cn * @date 2016年4月13日 下午7:47:28 * @param folderPath * @param file * @param zos */ private static void zipFile(String folderPath, File file, ZipOutputStream zos) { int index = folderPath.lastIndexOf(File.separator); try { if (file.isDirectory()) {// 文件夹,递归遍历文件夹下文件压缩 File[] files = file.listFiles(); if (files != null) { for (File f : files) zipFile(folderPath, f, zos); } return; } zos.putNextEntry(new ZipEntry(file.getAbsolutePath().substring(index))); byte[] buffer = new byte[1024]; int length = -1; FileInputStream fis = new FileInputStream(file); while ((length = fis.read(buffer)) != -1) { zos.write(buffer, 0, length); } fis.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 解压文件.zip格式 * * @author flueky zkf@yitong.com.cn * @date 2016年4月13日 下午3:12:23 * @param zipFilePath * @param outFolderPath */ public static void unzipFile(String zipFilePath, String outFolderPath) { try { ZipFile zipFile = new ZipFile(zipFilePath); Enumeration<ZipEntry> zList = (Enumeration<ZipEntry>) zipFile.entries(); while (zList.hasMoreElements()) { ZipEntry zipEntry = zList.nextElement(); if (zipEntry.getName().startsWith("__MACOSX"))// Mac // 系统自带压缩工具生成,屏蔽 continue; if (!zipEntry.isDirectory()) {// 只解压文件 InputStream is = zipFile.getInputStream(zipEntry); StringBuffer name = new StringBuffer(outFolderPath); if (name.lastIndexOf(File.separator) != name.length() - 1) { name.append(File.separator); } name.append(zipEntry.getName()); File outFile = new File(name.toString()); if (!outFile.getParentFile().exists()) outFile.getParentFile().mkdirs(); outFile.createNewFile(); FileOutputStream fos = new FileOutputStream(outFile); int length = -1; byte[] buffer = new byte[1024]; while ((length = is.read(buffer)) != -1) { fos.write(buffer, 0, length); } // 关闭流 is.close(); fos.close(); } } zipFile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
1 0
- Java 压缩zip格式文件
- java压缩/解压缩zip格式文件
- java压缩/解压缩zip格式文件
- java压缩/解压缩zip格式文件
- 使用C#压缩解压zip格式文件
- c# 实现zip格式文件的压缩和解压缩
- java 解压缩zip 压缩zip
- 案例 1-8: 将指定文件压缩成 Zip 格式文件
- 手机中的Zip格式文件解压和文件夹的压缩
- 使用C#压缩解压rar和zip格式文件
- Java Zip压缩
- java 压缩zip
- JAVA zip 压缩
- Java压缩zip文件
- java zip压缩
- JAVA zip压缩
- JAVA压缩/解压ZIP
- java zip压缩
- java泛型
- eclipse设置的断点上有斜线
- Android Jni开发之Android Studio中使用Android.mk
- 自加与及逗号运算符
- storm源码之理解Storm中Worker、Executor、Task关系
- Java 压缩zip格式文件
- mybatis项目中常见的小错误
- jsp中Servlet处理请求后转到一个jsp页面的解决办法
- Hibernate与 MyBatis的比较
- html 的标签的使用
- 深入理解Android中的View
- 【BZOJ3407】[Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题【01背包】
- hibernate分页查询和方言
- 【编写自己的GUI】(三)EditText的实现