最近,一直在研究批量下载的问题,刚开始的想法是批量选择文件后,点击下载按钮,会弹出一个路径选择框,选择路径后,几个文件同时下载下来...研究了一段时间,发现自己是菜鸟,实现不鸟呀...
后来,是在没办法,就想实现批量打包下载,不管是文件夹,还是文件,都能批量下载.经过我一天半的努力,终于实现啦 :P
批量压缩文件夹/文件的代码如下:
因为我的文件夹是存在hadoop上的,为了办证数据的完整性,hadoop使用了CRC数据校验。会对每一个文件产生对应的filename.crc文件。故我需要识别出.crc文件,不对其进行打包...
后来,是在没办法,就想实现批量打包下载,不管是文件夹,还是文件,都能批量下载.经过我一天半的努力,终于实现啦 :P
批量压缩文件夹/文件的代码如下:
public void ZipFiles(java.io.File[] srcfile,java.io.File zipfile){
byte[] buf = new byte[1024];
try{
//create the zip file
org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(new FileOutputStream(zipfile));
out.setEncoding("gbk");
//Compress the files
for(int i = 0; i < srcfile.length;i++){
String path = srcfile[i].getName();
zip(out,srcfile[i],path);
}
//Complete the zip file
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
private void zip(org.apache.tools.zip.ZipOutputStream out,File f,String base){
try{
if(f.isDirectory()){
File[] f1 = f.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base+"/"));
base=base + "/";
for(int i = 0; i <f1.length; i++){
zip(out,f1[i],base + f1[i].getName());
}
}else{
String fileName=f.getName();
int index = 0;
index = fileName.lastIndexOf(".");
String sub = fileName.substring(index+1);
if(!sub.equals("crc")){
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream in = new FileInputStream(f);
byte[] buf = new byte[1024];
int b;
while((b = in.read(buf))>0){
out.write(buf,0,b);
}
in.close();
}
}
}catch(IOException e){
e.printStackTrace();
}
}
因为我的文件夹是存在hadoop上的,为了办证数据的完整性,hadoop使用了CRC数据校验。会对每一个文件产生对应的filename.crc文件。故我需要识别出.crc文件,不对其进行打包...