利用字节流文件生成包含多文件的zip文件
InputStream[] inputStreamsList = new InputStream[jsonArr.size()];
String[] fileNameList = new String[jsonArr.size()];
这里是前台获取的文件信息,到fastdfs中查询出相应文件流
for(int i=0;i<jsonArr.size();i++) {
JSONObject jsonObj = (JSONObject) jsonArr.get(i);
String fileName = jsonObj.getString("filename");
String fastdir = jsonObj.getString("fastdir");
String conf_filename = this.getClass().getResource("/db.properties").getPath();
ClientGlobal.init(conf_filename);
TrackerClient tracker = null;
tracker = new TrackerClient();
TrackerServer trackerServer = null;
trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = null;
storageClient = new StorageClient(trackerServer, storageServer);
byte[] b = storageClient.download_file("group1", fastdir);
InputStream inputStream = new ByteArrayInputStream(b);
inputStreamsList[i] =inputStream;
fileNameList[i]= fileName;
}
//调用zip打包方法
public static void zip(String[] fileNameList, InputStream[] inputStream,String zipath) throws Exception {
if (fileNameList==null||fileNameList.length==0) {
throw new IllegalArgumentException("fileNameList is empty !");
}
if (fileNameList.length != inputStream.length) {
throw new IllegalArgumentException("fileNameList length is not equals to inputStream length !");
}
if(inputStream==null||inputStream.length==0) {
throw new IllegalArgumentException("inputStream is empty !");
}
ZipOutputStream zipOut = null;
try {
//创建一个空白zip
createNewzip(zipath);
File dir = new File(zipath);
OutputStream out = new FileOutputStream(dir);
zipOut = new ZipOutputStream(out);
for (int i = 0; i < fileNameList.length; i++) {
if (null == inputStream[i]) {
return;
}
try {
zipOut.putNextEntry(new ZipEntry(fileNameList[i]));
byte[] b = new byte[1024];
int len = 0;
while((len = inputStream[i].read(b)) != -1) {
zipOut.write(b,0,len);
}
zipOut.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(inputStream[i] != null) {
inputStream[i].close();
}
}
}
} finally {
try {
zipOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//创建一个空白zip参考
https://www.cnblogs.com/nje19951205/p/11991395.html
利用字节流文件生成包含多文件的zip文件的更多相关文章
- 在Linux下查找文件内容包含某个特定字符串的文件
如何在Linux下查找文件内容包含某个特定字符串的文件? 我的目录下面有test1和test2两个文件夹,里面都含有很多文件,其中test2里面还包含一个test文件夹 我想请问的是,如何通过查找关键 ...
- Python组织文件 实践:将文件的不同版本备份为ZIP文件
功能:备份文件夹.能将文件的不同版本备份下来,并且每个有不同的名字 #! python3 # backupToZip.py - 备份文件的不同版本到压缩文件中 import zipfile,os #f ...
- python用zipfile模块打包文件或是目录、解压zip文件实例
#!/usr/bin/env python # -*- coding: utf-8 -*- from zipfile import * import zipfile #解压zip文件 def unzi ...
- proto文件生成对应的js和ts文件
安装protobuf 先要安装node.js,然后用npm安装protobuf npm install -g protobufjs 生成js文件 单个文件 pbjs -t static-module ...
- C++头文件的包含顺序研究
一.<Google C++ 编程风格指南>里的观点 公司在推行编码规范,领导提议基本上使用<Google C++ 编程风格指南>.其中<Google C++ 编程风格指南 ...
- C_文件包含.h文件和包含.c文件总结
很多人对C语言中的 “文件包含”都不陌生了,文件包含处理在程序开发中会给我们的模块化程序设计带来很大的好处,通过文件包含的方法把程序中的各个功能模块联系起来是模块化程序设计中的一种非常有利的手段. 文 ...
- core文件生成和路径设置
在程序崩溃时,内核会生成一个core文件,即程序最后崩溃时的内存映像,和程序调试信息. 之后可以通过gdb,打开core文件察看程序崩溃时的堆栈信息,可以找出程序出错的代码所在文件和函数. 1.cor ...
- 【grpc proto】python使用proto文件生成简易的服务端和客户端
1.安装python-grpc(注意,是grpcio包,不是grpc包!) pip install grpcio 2.编写.proto文件 grpc教程:http://doc.oschina.net/ ...
- 爬虫文件存储:txt文档,json文件,csv文件
5.1 文件存储 文件存储形式可以是多种多样的,比如可以保存成 TXT 纯文本形式,也可以保存为 Json 格式.CSV 格式等,本节我们来了解下文本文件的存储方式. 5.1.1 TXT文本存储 将数 ...
随机推荐
- Mach-O简介及实际应用
一.前言 在正题开始之前,我们先来聊聊iOS中的hook技术.一谈到hook,很多人首先想到的是runtime,runtime确实强大,但是它存在很多局限性: 1).侵入性:一旦hook了某个类的 ...
- 文件对比工具 diff cmp patch(没弄完) pr
diff不仅可以对比文件,而且可以对比文件夹中的文件. 解析: diff用在比对两个文件的差异,并且是以行为单位进行对比.一般用在ascii纯文本档的对比上. 例 在tmp中创建一个testpw文件夹 ...
- Dubbox离线约束地址
地址: http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- echarts.min.js的引入
(1)使用地址引入 <script src="https://cdn.bootcss.com/echarts/3.7.1/echarts.min.js"></sc ...
- 【JavaWeb项目】一个众筹网站的开发(九)邮件开发
Java官方支持邮件开发,Javax-mail jdk中默认没有,需要另外下载 apache的基于Javax-mail开发了commons-mail,更加简单高效,推荐使用 一.电子邮件接收和发送协议 ...
- css盒模型宽高混合计算calc
例如: .element{ width:calc(expression); } 兼容性:在IE9+.FF4.0+.Chrome19+.Safari6+都得到了较好支持,但是在移动端的支持不是很好. 其 ...
- php重定向说明
302 临时重定向 header("location:http://api.com/headline?" . http_build_query($_REQUEST)); 301 ...
- Shiro学习(13)RememberMe
Shiro提供了记住我(RememberMe)的功能,比如访问如淘宝等一些网站时,关闭了浏览器下次再打开时还是能记住你是谁,下次访问时无需再登录即可访问,基本流程如下: 1.首先在登录页面选中Reme ...
- CSScomb.js --- 自定义 CSS 编写风格配置文件
一.排序分类 1. content overflow position z-index display float ... 表示定位/布局的属性(content比较特殊,作为伪元素不可少的,经常放置于 ...
- $\mathcal{CSP-S}$,私は来ています
记事本 开个坑.背包dp我是真的一点也不会了... NOIP2014飞扬的小鸟 NOIP2018货币系统 11-4:$Countdown$ $to$ $the$ $tenth$ $day$ 上午 困的 ...