spring roo 开发meavn项目(六)文件夹及子文件夹下载



思路是利用文件压缩流下载,将文件夹以及子文件夹中的文件结构按照压缩文件格式读入,然后将生成的文件流下载到本地,成为压缩文件包,解压后就还原为原先的文件夹结构



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 block
				throw 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 block
				throw 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值