Web上传文件在线压缩的实现
在我们开发网站的过程中,如果用户上传的文件过大,由于考虑到服务器带宽问题,我们经常会在用户体验的界面中让他下载压缩后的文件,那么如何将用户传上去的文件实现在线压缩呢?不仅如何,你还可以通过SMB协议实现远程文件在线压缩(因为在大型网站中,Web集群服务器与文件服务器一般都是分开的),以下代码可以帮您实现:
//打包网络上的zip文件public void zip(String zipFileName,String filepath,String username,String pwd)throws Exception{ SmbFile f=new SmbFile("smb://"+username+":"+pwd+"@"+filepath);zip(zipFileName,username,pwd,f); } //打包网络文件上的多个文件或者文件夹public void zip(String zipFileName,String[] filepaths,String username,String pwd)throws Exception{String str="smb://"+username+":"+pwd+"@"+zipFileName;ZipOutputStream out=new ZipOutputStream(new SmbFileOutputStream(str)); for(int i=0;i<filepaths.length;i++){SmbFile inputFile=new SmbFile("smb://"+username+":"+pwd+"@"+filepaths[i]);zip(out,inputFile,"");}System.out.println("zip done"); out.close(); }public void zip(String zipFileName,String username,String pwd,SmbFile inputFile)throws Exception{ String str="smb://"+username+":"+pwd+"@"+zipFileName;ZipOutputStream out=new ZipOutputStream(new SmbFileOutputStream(str)); zip(out,inputFile,""); System.out.println("zip done"); out.close(); } ?