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文件的更多相关文章

  1. 在Linux下查找文件内容包含某个特定字符串的文件

    如何在Linux下查找文件内容包含某个特定字符串的文件? 我的目录下面有test1和test2两个文件夹,里面都含有很多文件,其中test2里面还包含一个test文件夹 我想请问的是,如何通过查找关键 ...

  2. Python组织文件 实践:将文件的不同版本备份为ZIP文件

    功能:备份文件夹.能将文件的不同版本备份下来,并且每个有不同的名字 #! python3 # backupToZip.py - 备份文件的不同版本到压缩文件中 import zipfile,os #f ...

  3. python用zipfile模块打包文件或是目录、解压zip文件实例

    #!/usr/bin/env python # -*- coding: utf-8 -*- from zipfile import * import zipfile #解压zip文件 def unzi ...

  4. proto文件生成对应的js和ts文件

    安装protobuf 先要安装node.js,然后用npm安装protobuf npm install -g protobufjs 生成js文件 单个文件 pbjs -t static-module ...

  5. C++头文件的包含顺序研究

    一.<Google C++ 编程风格指南>里的观点 公司在推行编码规范,领导提议基本上使用<Google C++ 编程风格指南>.其中<Google C++ 编程风格指南 ...

  6. C_文件包含.h文件和包含.c文件总结

    很多人对C语言中的 “文件包含”都不陌生了,文件包含处理在程序开发中会给我们的模块化程序设计带来很大的好处,通过文件包含的方法把程序中的各个功能模块联系起来是模块化程序设计中的一种非常有利的手段. 文 ...

  7. core文件生成和路径设置

    在程序崩溃时,内核会生成一个core文件,即程序最后崩溃时的内存映像,和程序调试信息. 之后可以通过gdb,打开core文件察看程序崩溃时的堆栈信息,可以找出程序出错的代码所在文件和函数. 1.cor ...

  8. 【grpc proto】python使用proto文件生成简易的服务端和客户端

    1.安装python-grpc(注意,是grpcio包,不是grpc包!) pip install grpcio 2.编写.proto文件 grpc教程:http://doc.oschina.net/ ...

  9. 爬虫文件存储:txt文档,json文件,csv文件

    5.1 文件存储 文件存储形式可以是多种多样的,比如可以保存成 TXT 纯文本形式,也可以保存为 Json 格式.CSV 格式等,本节我们来了解下文本文件的存储方式. 5.1.1 TXT文本存储 将数 ...

随机推荐

  1. ollvm 编译

    ollvm 的编译相对 llvm 更简单, 1:下载ollvm代码,去 https://github.com/obfuscator-llvm/obfuscator/tree/llvm-4.0 下载,并 ...

  2. TCP建立连接的三次握手和释放连接的四次挥手

    TCP建立连接时,为什么要进行三次握手? 举个打电话的例子: A : 你好我是A,你听得到我在说话吗 B : 听到了,我是B,你听到我在说话吗 A : 嗯,听到了 建立连接,开始聊天! 第一次握手 第 ...

  3. Linux 任务管理器(二)

    特殊文件与进程 fuser命令 [root@localhost home]# fuser -muv . 用户 进程号 权限 命令 /home: root kernel mount (root)/hom ...

  4. mysql 使用 insert ignore into和unique实现不插入重复数据功能

    执行插入操作,如果数据库中不存在才插入,否则就不插入 首先需要通过unique指定唯一约束字段 然后写sql语句的时候就写  insert ignore into () valuse () 这样就能实 ...

  5. eclipse 彻底修改复制后的项目名称

    1.项目右键 --> properties --> Web Project Settings --> 修改Context root 2.web.xml 3.工作空间中找到当前项目下. ...

  6. vue 使用Animate.css库

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. vue中watch简单使用

    watch是一个对象,具有键值对:键指被监听的数据,值指处理方式. 值类型包括以下三个: 第一个handler:其值是一个回调函数.即监听到变化时应该执行的函数. 第二个是deep:其值是true或f ...

  8. 每天一个Linux命令:pwd(3)

    pwd pwd是Print Working Directory的缩写,其功能是显示当前所在工作目录的全路径.主要用在当不确定当前所在位置时,通过pwd来查看当前目录的绝对路径 格式 pwd [选项] ...

  9. PHP ftp_get() 函数

    定义和用法 ftp_get() 函数从 FTP 服务器上下载一个文件并保存到本地一个文件中. 如果成功,该函数返回 TRUE.如果失败,则返回 FALSE. 语法 ftp_get(ftp_connec ...

  10. Shell基础(五):sed基本用法、使用sed修改系统配置、sed多行文本处理、sed综合脚本应用

    一.sed基本用法 目标: 本案例要求熟悉sed命令的p.d.s等常见操作,并结合正则表达式,完成以下任务: 1> 删除文件中每行的第二个.最后一个字符    2> 将文件中每行的第一个. ...