spring roo 开发meavn项目(六)文件夹及子文件夹下载
来源:互联网 发布:网络骗女性的骗术大全 编辑:程序博客网 时间:2024/05/22 08:25
思路是利用文件压缩流下载,将文件夹以及子文件夹中的文件结构按照压缩文件格式读入,然后将生成的文件流下载到本地,成为压缩文件包,解压后就还原为原先的文件夹结构
public class ZipCompress {static final int BUFFER = 8192; public static void compress(String[] origanUids,String[] parentUrls,String[] nameSet,OutputStream outputStream){ try {CheckedOutputStream cos = new CheckedOutputStream(outputStream, new CRC32()); ZipOutputStream out = new ZipOutputStream(cos); String basedir = ""; for(int i=0;i<origanUids.length;i++){ compress(origanUids[i],parentUrls[i],nameSet[i],out,basedir); } out.close();} catch (Exception e1) {// TODO Auto-generated catch blockthrow new RuntimeException(e1); } } public static void compress(String userId,String[] parentUrls,String[] nameSet,OutputStream outputStream){ try {CheckedOutputStream cos = new CheckedOutputStream(outputStream, new CRC32()); ZipOutputStream out = new ZipOutputStream(cos); String basedir = ""; for(int i=0;i<nameSet.length;i++){ compress(userId,parentUrls[i],nameSet[i],out,basedir); } out.close();} catch (Exception e1) {// TODO Auto-generated catch blockthrow new RuntimeException(e1); } } private static void compress(String userId,String parentUrl,String name,ZipOutputStream out,String basedir){ Record record = FileInfoDao.listFilesOnlyByCorrectUrl(userId, parentUrl, name); int isDir = record.getIntValue(FileInfo.isDir); String s3url = record.getStrValue(FileMD5.s3url); if(isDir==1){ compressDirectory(userId,parentUrl,name,out,basedir); }else{ compressFile(s3url,name,out,basedir); } } /** 压缩一个目录 */ private static void compressDirectory(String userId,String parentUrl,String name, ZipOutputStream out, String basedir) { String nextParentUrl = parentUrl+name+"/"; List<Record> records = FileInfoDao.listCurrentFilesByUrl(userId, nextParentUrl); if(records.size()==0){ ZipEntry entry = new ZipEntry( basedir + name + "/"); try {out.putNextEntry(entry);} catch (IOException e) {// TODO Auto-generated catch block throw new RuntimeException(e); } } for (int i = 0; i < records.size(); i++) { /* 递归 */ Record record = records.get(i); String nextname = record.getStrValue(FileInfo.name); compress(userId,nextParentUrl,nextname, out, basedir + name + "/"); } } /** 压缩一个文件 */ private static void compressFile(String s3url,String filename,ZipOutputStream out, String basedir) { IFile file = IFile.getFile(s3url); try { BufferedInputStream bis = new BufferedInputStream(file.getInputStream()); ZipEntry entry = new ZipEntry(basedir + filename); out.putNextEntry(entry); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } bis.close(); } catch (Exception e) { throw new RuntimeException(e); } } }
0 0
- spring roo 开发meavn项目(六)文件夹及子文件夹下载
- spring roo 开发meavn项目(四)文件操作:上传,下载,本地复制,删除文件
- spring roo 开发meavn项目(一)使用工具搭建项目
- spring roo 开发meavn项目(二)后台返回map,list自动转换json格式
- spring roo 开发meavn项目(三)分页读取数据示例
- spring roo 开发meavn项目(五)后台发送邮件功能
- 遍历文件夹及子文件夹
- Spring Roo开发初评
- Spring Roo开发初评
- MFC 删除文件夹及子文件夹源代码
- Ubuntu 更改文件夹及子文件夹权限
- 遍历文件夹及子文件夹 delphi
- Ubuntu 更改文件夹及子文件夹权限
- Ubuntu 更改文件夹及子文件夹权限
- Ubuntu 更改文件夹及子文件夹权限
- linux copy 复制文件夹及子文件夹
- 文件夹拷贝 子文件及文件夹
- Ubuntu 更改文件夹及子文件夹权限
- 快速幂取模(分治思想)
- 设计模式之装饰者模式
- 安卓测试apk中,解决手机安卓不了应用的问题
- UVa 253
- LeetCode : Valid Anagram
- spring roo 开发meavn项目(六)文件夹及子文件夹下载
- 九度OJ-1186:打印日期
- 仿伙伴云表格的筛选控件
- Bootstrap学习-网格系统
- 数据库语句 英文中意
- webstorm 2016(2016.11.25更新)11 12 (注册,激活,破解,码,一起支持正版,最新可用)
- unity接入咪咕SDK遇到的坑
- jstack分析
- C++ primer的初步浏览