本篇文章给大家分享的是有关怎么在java中利用压缩流实现压缩与解压,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

公司主营业务:成都做网站、成都网站制作、成都外贸网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出鹿邑免费做网站回馈大家。
Java是什么
Java是一门面向对象编程语言,可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序。
1.概念
压缩流可以将输入的数据变为压缩格式后进行输出,或者读取压缩格式的数据后,解压为正常数据。
2.压缩步骤
(1)生成一个压缩类对象,这个对象来自于一个".zip"的文件,通过它产生一ZipOutputStream对象;
(2)生成压缩对象入口,因为需要被压缩的文件不止一个。需要用ZipEntry方法生成压缩入口文件后才能放进压缩文件;
(3)用putNextEntry将压缩入口放入压缩文件;
(4)将文件内容写入了out.write(),将压缩入口和文件流关闭。
3.目录压缩
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipStreamExam2 {
public static void main(String[] args) {
try {
File file = new File("d:\\zipmultidir");
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream("d:\\zipmultidir.zip")));
zipDir(file, zos, file);
zos.flush();
zos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//压缩一个目录至zip文件
private static void zipDir(File dir, ZipOutputStream zos, File rootDir) throws IOException {
if (!dir.isDirectory())
return;
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
System.out.println(files[i].getAbsolutePath());
String now = files[i].getAbsolutePath();
String root = rootDir.getAbsolutePath();
String name = now.substring(root.length() + 1);
System.out.println(name);
FileInputStream fis = new FileInputStream(files[i]);
byte buf[] = new byte[1024];
int len = 0;
ZipEntry ze = new ZipEntry(name);
zos.putNextEntry(ze);
while ((len = fis.read(buf)) != -1) {
zos.write(buf, 0, len);
}
fis.close();
} else if (files[i].isDirectory()) {
zipDir(files[i], zos, rootDir);
}
}
}
}4.解压到目录
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* Created by test2 on 2016/8/19.
*/
public class ZipStreamExam3 {
public static void main(String[] args) {
try {
File srcFile = new File("d:\\zipmultidir.zip");
System.out.println(srcFile.getCanonicalPath());
String curDir = srcFile.getParent()+File.separator+"destDir"+File.separator;
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(srcFile)));
ZipEntry ze = null;
byte[] buf = new byte[1024];
int len = 0;
while ((ze = zipInputStream.getNextEntry()) != null) {
String filePath = curDir + ze.getName();
File destFile = new File(filePath);
File destDir = new File(destFile.getParent());
if(!destDir.exists()){
destDir.mkdirs();
}
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFile));
while ((len = zipInputStream.read(buf)) != -1) {
bufferedOutputStream.write(buf, 0, len);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
zipInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}以上就是怎么在java中利用压缩流实现压缩与解压,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
新闻名称:怎么在java中利用压缩流实现压缩与解压
本文来源:http://www.jxjierui.cn/article/ghhghj.html


咨询
建站咨询
