java之压缩流(ZipOutputStream)
一、文件压缩,是很有必要的,我们在进行文件,传输过程中,很多时候都是,都是单个文件单个文件发送接收,但是当数据量特别大,或者文件数量比较多的时候,这个时候就可以考虑文件压缩。
二、优势:文件压缩过后,只需要进行一次文件的传输就可以了。减少频繁发送的问题。缺点:文件大小会变大,如果传输过程中断了,风险较大。
三、实现:
/**
* 提供给用户使用的基本压缩类
* @param srcPath
* @param outPath
* @throws IOException
*/
public static void compressFile(String srcPath, String outPath) throws IOException {
//读取源文件
File srcFile = new File(srcPath);
//判断输出路径是否正确
File outFile = new File(outPath);
//如果只是路劲加入对应的压缩名称
if (outFile.isDirectory()) {
//用"/"作文判断标准
if (outPath.endsWith(File.separator)) {
outPath += srcFile.getName().split("\\.")[] + ".zip";
} else {
outPath += File.separator + srcFile.getName().split("\\.")[] + ".zip";
}
}
//读取文件流
FileOutputStream fileOutputStream = new FileOutputStream(outPath);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
//压缩文件
compressFile(srcFile, srcFile.getName(),zipOutputStream);
//关闭流
zipOutputStream.close();
fileOutputStream.close();
} /**
* 迭代方式进行文件压缩
* @param file
* @param fileName
* @param outputStream
* @throws IOException
*/
private static void compressFile(File file, String fileName, final ZipOutputStream outputStream) throws IOException {
//如果是目录
if (file.isDirectory()) {
//创建文件夹
outputStream.putNextEntry(new ZipEntry(fileName+"/"));
//迭代判断,并且加入对应文件路径
File[] files = file.listFiles();
Iterator<File> iterator = Arrays.asList(files).iterator();
while (iterator.hasNext()) {
File f = iterator.next();
compressFile(f, fileName+"/"+f.getName(), outputStream);
}
} else {
//创建文件
outputStream.putNextEntry(new ZipEntry(fileName));
//读取文件并写出
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] bytes = new byte[];
int n;
while ((n = bufferedInputStream.read(bytes)) != -) {
outputStream.write(bytes, , n);
}
//关闭流
fileInputStream.close();
bufferedInputStream.close();
}
}
四、测试:
public static void main(String[] args) throws IOException {
compressFile("D:\\srv", "D:\\");
}


五、效果还是可以,此方式根据需要修改!
java之压缩流(ZipOutputStream)的更多相关文章
- Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)
Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...
- [源码]ObjectIOStream 对象流 ByteArrayIOStream 数组流 内存流 ZipOutputStream 压缩流
1.对象流 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File ...
- java压缩流
java压缩流是为了减少传输时的数据量,可以将文件压缩成ZIP.JAR.GZIP等文件格式.
- java文件压缩ZipOutPutStream
其实最好的书籍就是javaAPI 1.创建ZipOutPutStream流,利用BufferedOutputStream提个速. 2.新建zip方法,用来压缩文件,传参 3.zip方法利用putNex ...
- Java IO--压缩流
压缩流: 压缩流的实现: zipEntry: 在实例化ZipEntry的时候,要设置名称,此名称实际上就是压缩文件中的每一个元素的名称. ZipOutputStream: import java.io ...
- Java生成压缩文件(zip、rar 格式)
jar坐标: <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</ar ...
- 吴裕雄--天生自然JAVAIO操作学习笔记:压缩流与回退流
import java.io.File ; import java.io.FileInputStream ; import java.io.InputStream ; import java.util ...
- 面试必备:详解Java I/O流,掌握这些就可以说精通了?
@TOC Java IO概述 IO就是输入/输出.Java IO类库基于抽象基础类InputStream和OutputStream构建了一套I/O体系,主要解决从数据源读入数据和将数据写入到目的地问题 ...
- java ZIP压缩文件
问题描述: 使用java ZIP压缩文件和目录 问题解决: (1)单个文件压缩 注: 以上是实现单个文件写入压缩包的代码,注意其中主要是在ZipOutStream流对象中创建Z ...
随机推荐
- POJ 3259 Wormholes(Bellman-Ford)
http://poj.org/problem?id=3259 题意:有一些普通的洞和虫洞,每个洞都有经过的时间,虫洞的时间是负的,也就是时光倒流,问是否能回到出发时的时间. 思路: 贝尔曼-福特算法判 ...
- codeforces 354 div2 C Vasya and String 前缀和
C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Docker常用命令汇总,和常用操作举例
Docker命令 docker 常用命令如下 管理命令: container 管理容器 image 管理镜像 network 管理网络 node 管理Swarm节点 plugin 管理插件 secre ...
- node 常用指令 node 扩展链接
node -v node 版本 npm -v npm版本号,npm是在安装nodejs时一同安装的nodejs包管理器 (注册.安装模块,和小乌龟有点像) npm list 当 ...
- 【Docker】Segmentation Fault or Critical Error encountered. Dumping core and abort
背景 CentOS7 安装Docker后,load镜像时出现以下错误: Segmentation Fault or Critical Error encountered. Dumping core a ...
- linux下升级npm以及node
npm升级 废话不多说,直接讲步骤.先从容易的开始,升级npm. npm这款包管理工具虽然一直被人们诟病,很多人都推荐使用yarn,但其使用人数还是不见减少,况且npm都是随node同时安装好的,一时 ...
- linux下smb
smbclient用法 1,列出某个IP地址所提供的共享文件夹smbclient -L 198.168.0.1 -U username%password 2,像FTP客户端一样使用smbclients ...
- 获取display:none的元素的宽度和高度
display为none的元素不能通过offsetWidth和offsetHeight来获取宽高(未参与css渲染), 解决方案:可以通过在display为none的元素使用行内样式style设置宽高 ...
- linux 命令 --while
shell while 语法: while [条件判断式] do 程序 done 对 while 循环来讲,只要条件判断式成立,循环就会一直进行,直到条件判断式不成立,循环才会停止. 例如: 循环从1 ...
- 身份证&银行卡识别方案
一. 调用第三方服务 腾讯云OCR识别: 实现方法:Post图片 URL到腾讯云服务器.Post图片文件 到腾讯云服务器 b. 报价: 月接口调用总量 0<调用量≤1000 1000&l ...