JAVA中压缩与解压缩
以压缩Zip文件为例。主要是通过ZipOutputStream类实现。解压缩主要使用ZipFile类和ZipInputStream以及ZipEntry类。
package main; import java.io.*;
import java.util.*;
import java.util.zip.*;
public class Main
{ public static final Integer BUFFERSIZE = 1024*1024;
public static void main(String[] args) throws Exception
{
File fileNeedToBeCompressed = new File("C:" + File.separator + "D" + File.separator + "code" + File.separator + "output.xml");
StringBuffer zipFilePath = new StringBuffer(fileNeedToBeCompressed.getParent());
zipFilePath.append(File.separator).append(getFileName(fileNeedToBeCompressed.getName())).append(".zip");
File compressedFile = new File(zipFilePath.toString()); InputStream is = null;
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(compressedFile)); if(fileNeedToBeCompressed.isDirectory())
{
//压缩一个文件夹 File[] files = fileNeedToBeCompressed.listFiles();
for(File file : files)
{
compressFile(file, is, zos);
}
}
else
{
//压缩一个文件 compressFile(fileNeedToBeCompressed,is,zos);
}
zos.close(); System.out.println("///~ Main done");
} public static String getFileName(String fileName)
{
if(fileName.lastIndexOf('.') < 0)
{
return fileName;
}
else
{
return fileName.substring(0,fileName.lastIndexOf('.'));
}
} public static void compressFile(File fileNeedToBeCompressed,InputStream is,ZipOutputStream zos) throws Exception
{
System.out.println("正在压缩:" + fileNeedToBeCompressed.getName());
is = new FileInputStream(fileNeedToBeCompressed);
zos.putNextEntry(new ZipEntry(fileNeedToBeCompressed.getName()));
int bytesReaded = 0;
byte[] buffer = new byte[BUFFERSIZE];
while((bytesReaded = is.read(buffer)) > 0)
{
zos.write(buffer,0,bytesReaded);
}
is.close();
} }
ZipInputStream 获取压缩文件中的每个ZipEntry,然后ZipFile通过ZipEntry拿到输入流。
package main; import java.io.*;
import java.util.*;
import java.util.zip.*;
public class Main
{ public static final Integer BUFFERSIZE = 1024*1024;
public static void main(String[] args) throws Exception
{
// {
// File fileNeedToBeCompressed = new File("C:" + File.separator + "D" + File.separator + "code" + File.separator + "resource");
// StringBuffer zipFilePath = new StringBuffer(fileNeedToBeCompressed.getParent());
// zipFilePath.append(File.separator).append(getFileName(fileNeedToBeCompressed.getName())).append(".zip");
// File compressedFile = new File(zipFilePath.toString());
//
//
// InputStream is = null;
// ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(compressedFile));
//
// if(fileNeedToBeCompressed.isDirectory())
// {
// //压缩一个文件夹
// File[] files = fileNeedToBeCompressed.listFiles();
// for(File file : files)
// {
// compressFile(file, is, zos);
// }
// }
// else
// {
// //压缩一个文件
//
// compressFile(fileNeedToBeCompressed,is,zos);
// }
// zos.close();
// } {
String zipFilePath = "C:" + File.separator + "D" + File.separator + "code" + File.separator + "resource.zip";
ZipFile zipFile = new ZipFile(zipFilePath);
ZipInputStream zis = new ZipInputStream(new FileInputStream(new File(zipFilePath)));
ZipEntry tempZipEntry = null;
OutputStream os = null;
byte[] buffer = new byte[BUFFERSIZE];
File unzipedFolder = new File(getFileName(zipFile.getName()));
int bytesReaded = 0;
if(!unzipedFolder.exists())
unzipedFolder.mkdirs();
while((tempZipEntry = zis.getNextEntry())!= null)
{
System.out.println("正在解压:" + tempZipEntry.getName());
File newFile = new File(unzipedFolder.getPath() + File.separator + tempZipEntry.getName());
InputStream is = zipFile.getInputStream(tempZipEntry);
os = new FileOutputStream(newFile);
while(( bytesReaded = is.read(buffer)) > 0)
{
os.write(buffer, 0, bytesReaded);
}
os.close();
}
zipFile.close();
zis.close();
}
System.out.println("///~ Main done");
} public static String getFileName(String fileName)
{
if(fileName.lastIndexOf('.') < 0)
{
return fileName;
}
else
{
return fileName.substring(0,fileName.lastIndexOf('.'));
}
} public static void compressFile(File fileNeedToBeCompressed,InputStream is,ZipOutputStream zos) throws Exception
{
System.out.println("正在压缩:" + fileNeedToBeCompressed.getName());
is = new FileInputStream(fileNeedToBeCompressed);
zos.putNextEntry(new ZipEntry(fileNeedToBeCompressed.getName()));
int bytesReaded = 0;
byte[] buffer = new byte[BUFFERSIZE];
while((bytesReaded = is.read(buffer)) > 0)
{
zos.write(buffer,0,bytesReaded);
}
is.close();
} }
JAVA中压缩与解压缩的更多相关文章
- linux中压缩与解压缩命令小结
linux中压缩与解压操作非常常见,其命令参数也非常的多,这里只介绍最经常用的带打包文件的几种压缩和解压方式和几个最常用的参数. 现在最常用的压缩和解压工具是gzip和bzip2,这两种工具不能相互解 ...
- Java实现压缩与解压缩
import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...
- java 版本压缩、解压缩zip
import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...
- java GZIP压缩与解压缩
1.GZIP压缩 public static byte[] compress(String str, String encoding) { if (str == null || str.length( ...
- linux中压缩与解压缩命令
.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...
- linux中压缩、解压缩命令详解
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- java ZipOutputStream压缩文件,ZipInputStream解压缩
java中实现zip的压缩与解压缩.java自带的 能实现的功能比较有限. 本程序功能:实现简单的压缩和解压缩,压缩文件夹下的所有文件(文件过滤的话需要对File进一步细节处理). 对中文的支持需要使 ...
- JAVA中的deflate压缩实现
在文件的传输过程中,为了使大文件能够更加方便快速的传输,一般采用压缩的办法来对文件压缩后再传输,JAVA中的java.util.zip包中的Deflater和Inflater类为使用者提供了DEFLA ...
- 利用JAVA API函数实现数据的压缩与解压缩
综述 许多信息资料都或多或少的包含一些多余的数据.通常会导致在客户端与服务器之间,应用程序与计算机之间极大的数据传输量.最常见的解决数据存储和信息传送的方法是安装额外的存储设备和扩展现有的通讯能力 ...
随机推荐
- oracle进程
http://blog.csdn.net/leshami/article/details/5529239 Oracle实例和Oracle数据库(Oracle体系结构) 几类进程:用户进程,服务进程,后 ...
- OpenJudge计算概论-扩号匹配问题【这个用到了栈的思想】
/*====================================================================== 扩号匹配问题 总时间限制: 1000ms 内存限制: ...
- mybatis 中的稍微复杂些的sql语句
mybatis 中的稍微复杂些的sql语句: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP ...
- SQLSERVER 605 尝试在数据库 %d 中提取逻辑页 %S_PGID 失败。 该逻辑页属于分配单元 %I64d,而非 %I64d。
今天在开发过程中写了一个存储过程发现执行的时候,时不时会提示605错误,重新执行又可能会成功. 百度了一下,很多说法是硬件的IO问题,就是存储器反馈给SQL SERVER 写入成功,但下次读取的时候S ...
- linux ubuntu系统下,adb不是内部命令 (如何才能让adb命令可以使用)
linux ubuntu系统下,adb不是内部命令 原文地址 linux ubuntu系统下,adb不是内部命令 解决方法: 1.sudo gedit ~/.bashrc 2.将下面的两句加到上面打开 ...
- MyBatis插入多条
<insert id="insertProjectPropertyRelList" parameterType="java.util.List"> ...
- (转载)CentOS6下 源代码方式安装openERP7.0
CentOS6下 源代码方式安装openERP7.0 安装背景 :CPU32 bit,CentOS 6.4版本,openERP7.0,linux shell为bash,PostgreSQL9.2 1. ...
- [转]Hibernate重要规则总结
实体类的编写规则 l 实体类必须具备无参构造方法 l 实体类必须具备数据库标识 l 通常选用无业务意义的逻辑主键作为数据库标识,通常是int/long/Str ...
- .NET_RSA加密全接触(重、难点解析)
.NET_RSA加密全接触(重.难点解析) .NET Framework提供了两个类供我们使用RSA算法,分别是:用于加密数据的RSACryptoServiceProvider和用于数字签名的DSAC ...
- 【python】浅谈包
python中的包可以理解为模块的集合.每个包也既可以为单包也可以有多个小包组成. Python中的package定义很简单,其层次结构与目录的层次结构相同,但是每个package必须包含一个__in ...