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