java压缩文件方法
2014-02-12 13:26
99 查看
/** * 压缩文件 * @param srcfile File[] 需要压缩的文件列表 * @param zipfile File 压缩后的文件 */ public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) { byte[] buf = new byte[1024]; try { // Create the ZIP file ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); //ZipOutputStream out = new ZipOutputStream(Response.getOutputStream());--设置成这样可以不用保存在本地,再输出, 通过response流输出。 // Compress the files for (int i = 0; i < srcfile.length; i++) { FileInputStream in = new FileInputStream(srcfile[i]); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(srcfile[i].getName())); // Transfer bytes from the file to the ZIP file int len; while ( (len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); System.out.println("压缩完成."); }catch (IOException e) { e.printStackTrace(); } }
相关文章推荐
- Java实现自动压缩文件并加密的方法示例
- Java处理压缩文件或文件夹相关的一些方法的实现
- java创建压缩文件方法(包括文件夹下包含文件夹)
- Java压缩解压缩文件的几种方法
- java 压缩打包文件的实现方法
- 实例展示使用Java压缩和解压缩7z文件的方法
- java 中压缩和解压文件的方法
- JAVA实现压缩文件的方法(使用java.util.zip.*)
- java解压zip或rar压缩文件(有源码和jar包以及对中文乱码的处理方法)
- 实例展示使用Java压缩和解压缩7z文件的方法
- java压缩文件夹或文件的方法
- Java微信浏览器上传文件使用Base64方法(增加压缩上传方法)
- Java压缩文件工具类ZipUtil使用方法代码示例
- 非java.util.zip,使用ant.jar制作zip压缩文件,以及相关中文解决方法
- Java采用readInt()方法直接读取特定的ZIP压缩文件出现了奇怪的问题
- java后台批量下载文件并压缩成zip下载的方法
- Java 针对文件与文件夹进行压缩的方法
- Java实现多文件压缩打包的方法
- Java方法进行解压/生成有密码保护的压缩文件(zip格式)
- Java创建ZIP压缩文件的方法