/** * 将文件压缩到ZIP输出流 * * @param dirPath 目录路径 * @param file 文件 * @param zouts 输出流 */ public static void zipFilesToZipFile(String dirPath, File file, ZipOutputStream zouts) { FileInputStream fin = null; ZipEntry entry = null; // 创建复制缓冲区 byte[] buf = new byte[4096]; int readByte = 0; if (file.isFile()) { try { // 创建一个文件输入流 fin = new FileInputStream(file); // 创建一个ZipEntry entry = new ZipEntry(getEntryName(dirPath, file)); // 存储信息到压缩文件 zouts.putNextEntry(entry); // 复制字节到压缩文件 while ((readByte = fin.read(buf)) != -1) { zouts.write(buf, 0, readByte); } zouts.closeEntry(); fin.close(); System.out.println("添加文件 " + file.getAbsolutePath() + " 到zip文件中!"); } catch (Exception e) { e.printStackTrace(); } } }
/** * 将目录压缩到ZIP输出流 * * @param dirPath 目录路径 * @param fileDir 文件信息 * @param zouts 输出流 */ public static void zipDirectoryToZipFile(String dirPath, File fileDir, ZipOutputStream zouts) { if (fileDir.isDirectory()) { File[] files = fileDir.listFiles(); // 空的文件夹 if (files.length == 0) { // 目录信息 ZipEntry entry = new ZipEntry(getEntryName(dirPath, fileDir)); try { zouts.putNextEntry(entry); zouts.closeEntry(); } catch (Exception e) { e.printStackTrace(); } return; } for (File file : files) { if (file.isFile()) { // 如果是文件,则调用文件压缩方法 FileUtils.zipFilesToZipFile(dirPath, file, zouts); } else { // 如果是目录,则递归调用 FileUtils.zipDirectoryToZipFile(dirPath, file, zouts); } } } }
/** * Adds a single file into the ZipOutputStream with specified entry name. * * @throws IOException */ private void addFileToZip(ZipOutputStream zipOut, UploadedFile file, String name) throws IOException { if (log.isTraceEnabled()) { log.trace(">> addFileToZip(): " + file); } ZipEntry entry = new ZipEntry(name); zipOut.putNextEntry(entry); InputStream in = null; try { in = file.getInputstream(); FileUtils.copyStream(file.getInputstream(), zipOut); zipOut.closeEntry(); } finally { FileUtils.close(in); } // try (InputStream in = file.getInputstream()) { // FileUtils.copyStream(file.getInputstream(), zipOut); // zipOut.closeEntry(); // } if (log.isTraceEnabled()) { log.trace("<< addFileToZip()"); } }
private void zipFiles(List<String> srcfile, ByteArrayOutputStream bos) { try { ZipOutputStream out = new ZipOutputStream(bos); out.setEncoding("GBK"); for (int i = 0; i < srcfile.size(); i++) { out.putNextEntry(new ZipEntry(srcfile.get(i))); out.write(bos.toByteArray()); } out.closeEntry(); out.flush(); out.finish(); out.close(); } catch (IOException e) { e.printStackTrace(); logger.error("ZipUtil zipFiles exception:" + e); } }
/** * Adds a single file into the ZipOutputStream with specified entry name. * * @throws IOException */ private void addFileToZip(ZipOutputStream zipOut, ReshakaUploadedFile file, String name) throws IOException { if (log.isTraceEnabled()) { log.trace(">> addFileToZip(): " + file); } ZipEntry entry = new ZipEntry(name); zipOut.putNextEntry(entry); try (InputStream in = file.getInputstream()) { FileUtils.copyStream(file.getInputstream(), zipOut); zipOut.closeEntry(); } if (log.isTraceEnabled()) { log.trace("<< addFileToZip()"); } }
/** * 压缩文件 * * @param srcfile File[] 需要压缩的文件列表 * @param zipfile File 压缩后的文件 */ public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) { byte[] buf = new byte[1024]; try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); for (int i = 0; i < srcfile.length; i++) { FileInputStream in = new FileInputStream(srcfile[i]); out.putNextEntry(new ZipEntry(srcfile[i].getName())); String str = srcfile[i].getName(); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); System.out.println("压缩完成."); } catch (IOException e) { e.printStackTrace(); } }
/** 压缩文件zip-Apache中ant所带包 @功能信息 : @参数信息 : @返回值信息 : @异常信息 : */ public static String getZip(List list, String path, String fileName) throws Exception { byte[] buffer = new byte[1024]; String strZipName = fileName + ".zip"; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(path + strZipName)); for (int j = 0; j < list.size(); j++) { String name = list.get(j).toString(); FileInputStream fis = new FileInputStream(path + name); out.putNextEntry(new ZipEntry(name)); int len; while ((len = fis.read(buffer)) > 0) { out.write(buffer, 0, len); } out.closeEntry(); fis.close(); new File(path + name).delete(); } out.close(); String zipFilePath = path + strZipName; // System.out.println(zipFilePath+"----"); // System.out.println("生成Demo.zip成功"); return zipFilePath; }