java putnextentry-凯发k8官方网
java对zip格式压缩和解压缩
通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压。
1.1 zip和gzip的区别
gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格式),它的设计目标是处理单个的文件。gzip在压缩文件中的数据时使用的就是zlib。为了保存与文件属性有关的信息,gzip需要在压缩文件(*.gz)中保存更多的头信息内容,而zlib不用考虑这一点。但gzip只适用于单个文件,所以我们在unix/linux上经常看到的压缩包后缀都是*.tar.gz或*.tgz,也就是先用tar把多个文件打包成单个文件,再用gzip压缩的结果。
zip只是一种数据结构,跟rar同类型。zip是适用于压缩多个文件的格式(相应的工具有pkzip和winzip等),因此,zip文件还要进一步包含文件目录结构的信息,比gzip的头信息更多。但需要注意,zip格式可采用多种压缩算法,我们常见的zip文件大多不是用zlib的算法压缩的,其压缩数据的格式与gzip大不一样。
1.2相关类与接口:
checksum接口:表示数据校验和的接口,被类adler32和crc32实现
adler32 :使用alder32算法来计算checksum数目
crc32:使用crc32算法来计算checksum数目
checkedinputstream:inputstream派生类,可得到输入流的校验和checksum,用于校验数据的完整性
checkedoutputstream :outputstream派生类,可得到输出流的校验checksum, 用于校验数据的完整性
deflateroutputstream :压缩类的基类。
zipoutputstream:deflateroutputstream的一个子类,把数据压缩成zip文件格式
gzipoutputstream :deflateroutputstream的一个子类,把数据压缩成gzip文件格式
inflaterinputstream:解压缩类的基类
zipinputstream:inflaterinputstream的一个子类,能解压缩zip格式的数据
gzipinputstream:inflaterinputstream的一个子类,能解压缩zip格式的数据
zipentry 类:表示 zip 文件条目
zipfile 类:此类用于从 zip 文件读取条目
1.3 压缩文件
下面实例我们使用了apache的zip工具包(所在包为ant.jar ),因为java类型自带的不支持中文路径,不过两者使用的方式是一样的,只是apache压缩工具多了设置编码方式的接口,其他基本上是一样的。另外,如果使用org.apache.tools.zip.zipoutputstream来压缩的话,我们只能使用org.apache.tools.zip.zipentry来解压,而不能使用java.util.zip.zipinputstream来解压读取了,当然apache并未提供zipinputstream类。
publicstaticvoidcompress(string srcfilepath, string destfilepath) {
file src= newfile(srcfilepath);
if(!src.exists()) {
thrownewruntimeexception(srcfilepath "不存在");
}
file zipfile= newfile(destfilepath);
try{
fileoutputstream fos = new fileoutputstream(zipfile);
checkedoutputstream cos = new checkedoutputstream(fos, new crc32());
zipoutputstream zos = new zipoutputstream(cos);
string basedir = "";
compressbytype(src, zos, basedir);
zos.close();
} catch(exception e) {
// todoauto-generated catch block
e.printstacktrace();
}
}
privatestaticvoidcompressbytype(file src, zipoutputstream zos,
string basedir) {
if(!src.exists())
return;
system.out.println("压缩" basedir src.getname());
if(src.isfile()) {
compressfile(src, zos, basedir);
} elseif(src.isdirectory()) {
compressdir(src, zos, basedir);
}
}
/**
*压缩文件
*
*/
privatestaticvoidcompressfile(file file, zipoutputstream zos,
string basedir) {
if(!file.exists())
return;
try{
bufferedinputstream bis = new bufferedinputstream(
new fileinputstream(file));
zipentry entry = new zipentry(basedir file.getname());
zos.putnextentry(entry);
int count;
byte[] buf = new byte[bufsize];
while ((count = bis.read(buf)) != -1) {
zos.write(buf, 0, count);
}
bis.close();
} catch(exception e) {
// todo: handle exception
}
}
/**
*压缩文件夹
*
*/
privatestaticvoidcompressdir(file dir, zipoutputstream zos,
string basedir) {
if(!dir.exists())
return;
file[] files= dir.listfiles();
if(files.length == 0){
try {
zos.putnextentry(new zipentry(basedir dir.getname()
file.separator));
} catch (ioexception e) {
e.printstacktrace();
}
}
for(file file: files) {
compressbytype(file, zos, basedir dir.getname() file.separator);
}
}
总结步骤:
创建压缩到的文件file zipfile= newfile(destfilepath);
根据zipfile生成zipoutputstream用于写入即将被压缩的文件
fileoutputstream fos = new fileoutputstream(zipfile);
checkedoutputstream cos = new checkedoutputstream(fos, new crc32());
zipoutputstream zos = new zipoutputstream(cos);
循环遍历源文件,首先需要创建zipentry用于标记压缩文件中含有的条目
zipentry entry = new zipentry(basedir file.getname());
然后将条目增加到zipoutputstream中,
zos.putnextentry(entry);
最后再调用要写入条目对应文件的输入流读取文件内容写入到压缩文件中。
bufferedinputstream bis = new bufferedinputstream(
new fileinputstream(file));
zipentry entry = new zipentry(basedir file.getname());
zos.putnextentry(entry);
int count;
byte[] buf = new byte[bufsize];
while ((count = bis.read(buf)) != -1) {
zos.write(buf, 0, count);
}
注意:如果是空目录直接zos.putnextentry(new zipentry(basedir dir.getname() file.separator))并不用写入文件内容,其中最主要的涉及到目录的压缩的,就是这一句话 out.putnextentry(new zipentry(base "/")); //放入一级目录 (防止空目录被丢弃)
1.4解压zip文件
publicstaticvoiddecompress(string srcpath, string dest) throwsexception {
file file= newfile(srcpath);
if(!file.exists()) {
thrownewruntimeexception(srcpath "所指文件不存在");
}
zipfile zf= newzipfile(file);
enumeration
entries= zf.getentries();
zipentry
entry= null;
while(entries.hasmoreelements()) {
entry= (zipentry) entries.nextelement();
system.out.println("解压" entry.getname());
if(entry.isdirectory()) {
string dirpath= dest file.separator entry.getname();
file dir= newfile(dirpath);
dir.mkdirs();
} else{
//表示文件
file f= newfile(dest file.separator entry.getname());
if(!f.exists()) {
string dirs= fileutils.getparentpath(f);
file parentdir= newfile(dirs);
parentdir.mkdirs();
}
f.createnewfile();
//将压缩文件内容写入到这个文件中
inputstream is= zf.getinputstream(entry);
fileoutputstream fos= newfileoutputstream(f);
intcount;
byte[] buf= newbyte[8192];
while((count= is.read(buf)) != -1) {
fos.write(buf, 0, count);
}
is.close();
fos.close();
}
}
}
总结
以上是凯发k8官方网为你收集整理的java putnextentry_java对zip格式压缩和解压缩的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: