java 版本压缩、解压缩zip
import java.io.*;
import java.util.*;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class FileMgr {
public FileMgr() {
}
/**
* 压缩文件
* @param srcfile File[] 需要压缩的文件列表
* @param zipfile File 压缩后的文件
*/
public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
byte[] buf = new byte[1024];
try {
// Create the ZIP file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
// Compress the files
for (int i = 0; i < srcfile.length; i++) {
FileInputStream in = new FileInputStream(srcfile[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(srcfile[i].getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
System.out.println("压缩完成.");
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解压缩
* @param zipfile File 需要解压缩的文件
* @param descDir String 解压后的目标目录
*/
public static void UnZipFiles(java.io.File zipfile, String descDir) {
try {
// Open the ZIP file
ZipFile zf = new ZipFile(zipfile);
for (Enumeration entries = zf.entries(); entries.hasMoreElements(); ) {
// Get the entry name
ZipEntry entry = ( (ZipEntry) entries.nextElement());
String zipEntryName = entry.getName();
InputStream in = zf.getInputStream(entry);
// System.out.println(zipEntryName);
OutputStream out = new FileOutputStream(descDir + zipEntryName);
byte[] buf1 = new byte[1024];
int len;
while ( (len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
// Close the file and stream
in.close();
out.close();
System.out.println("解压缩完成.");
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
java 版本压缩、解压缩zip的更多相关文章
- java压缩/解压缩zip格式文件
因为项目要用到压缩.解压缩zip格式压缩包,只好自己封装一个,对于网上流行的中文乱码的问题,本文的解决方法是用apache的包代替jdk里的.基本上还是比较好用的. 废话少说,直接上代码. } ...
- JAVA压缩 解压缩zip 并解决linux下中文乱码
1. [代码][Java]代码 1:再压缩前,要设置linux模式, 需要使用第三方ant-1.6.5.jar 如果是文件目录,则ZipEntry zipEntry=new ZipEntry(b ...
- AntZipUtils【基于Ant的Zip压缩解压缩工具类】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 Android 压缩解压zip文件一般分为两种方式: 基于JDK的Zip压缩工具类 该版本存在问题:压缩时如果目录或文件名含有中文, ...
- Linux的压缩/解压缩文件处理 zip & unzip
Linux的压缩/解压缩命令详解及实例 压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip 另:有些服 ...
- java ZipOutputStream压缩文件,ZipInputStream解压缩
java中实现zip的压缩与解压缩.java自带的 能实现的功能比较有限. 本程序功能:实现简单的压缩和解压缩,压缩文件夹下的所有文件(文件过滤的话需要对File进一步细节处理). 对中文的支持需要使 ...
- Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php
Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php 1. Jdk zip 跟apache ant zip 1 2. Apache Ant包进行ZIP文件压缩,upzip ...
- Android zip文件压缩解压缩
DirTraversal.java <P style="TEXT-ALIGN: left; PADDING-BOTTOM: 0px; WIDOWS: 2; TEXT-TRANSFORM ...
- Java实现压缩文件与解压缩文件
由于工作需要,需要将zip的压缩文件进行解压,经过调查发现,存在两个开源的工具包,一个是Apache的ant工具包,另一个就是Java api自带的工具包:但是Java自带的工具包存在问题:如果压缩或 ...
- java实现将文件压缩成zip格式
以下是将文件压缩成zip格式的工具类(复制后可以直接使用): zip4j.jar包下载地址:http://www.lingala.net/zip4j/download.php package util ...
随机推荐
- 系统启动时队列自动下单--ServletContextListener
package com.liying.pear.queue; import javax.servlet.ServletContextEvent; import javax.servlet.Servle ...
- linux开机自启动设置,自定义开机启动模版,nginx开机自启动服务
/etc/init.d 目录,我们把shell脚本放在这个目录下来作为启动脚本 都是用来放服务脚本的,当Linux启动时,会寻找这些目录中的服务脚本,并根据脚本的run level确定不同的启动级别. ...
- 最短路径算法----floyd(转)
一.Floyd算法 假设从i到j的最短路径上要经过若干个顶点,这些中间顶点中最大的顶点编号为k,最小的顶点为t,因此要求算dist[i][j]的最小值,那么只需要求算dist[i][s]+dist[s ...
- 原型模式Prototype,constructor,__proto__详解
最近由于在找工作,又拿起<JavaScript高级程序设计>看了起来,从中也发现了自己确实还是有很多地方不懂,刚刚看到原型模式这里,今天终于搞懂了,当然,我也不知道自己的理解是否有错. 1 ...
- C#Redis 事务操作
一.理论 还是抄前辈的理论知识. 和众多其它数据库一样,Redis作为NoSQL数据库也同样提供了事务机制.在Redis中,MULTI/EXEC/DISCARD/WATCH这四个命令是我们实现事务的基 ...
- ASP.NET MVC提交一个较复杂对象至WCF Service
前一篇<jQuery.Ajax()执行WCF Service的方法>http://www.cnblogs.com/insus/p/3727875.html 我们有练习在asp.net mv ...
- Spring基础(6) : 普通Bean对象中保存ApplicationContext
public class Person implements ApplicationContextAware{ ApplicationContext context; public String na ...
- 0 or 1(hdu2608)数学题
0 or 1 Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 查看centos操作系统、java_jdk、hadoop位数
1.centos操作系统位数: file /bin/ls 此为64位 2.java jdk 位数: java -version 这个是64为的,若是没有,则默认为32位 3.hadoop位数: 进入h ...
- php静态变量与方法与phar的使用
本节用类与静态变量改造之前的例子:php根据命令行参数生成配置文件 ghostinit.php: <?php class ghostinit{ static $version = 'ghost ...