Java Code Examples for org.apache.tools.zip.ZipOutputStream#closeEntry()
The following examples show how to use
org.apache.tools.zip.ZipOutputStream#closeEntry() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZipUtils.java From joylau-springboot-daemon-windows with MIT License | 6 votes |
/** * <p> * 压缩文件 * </p> * * @param sourceFolder 压缩文件夹 * @param zipFilePath 压缩文件输出路径 */ public static void zip(String sourceFolder, String zipFilePath) throws Exception { OutputStream out = new FileOutputStream(zipFilePath); BufferedOutputStream bos = new BufferedOutputStream(out); ZipOutputStream zos = new ZipOutputStream(bos); // 解决中文文件名乱码 zos.setEncoding(CHINESE_CHARSET); File file = new File(sourceFolder); String basePath = null; if (file.isDirectory()) { basePath = file.getPath(); } else { basePath = file.getParent(); } zipFile(file, basePath, zos); zos.closeEntry(); zos.close(); bos.close(); out.close(); }
Example 2
Source File: Files.java From development with Apache License 2.0 | 6 votes |
public void writeTo(OutputStream out) throws IOException { final Set<String> filenames = new HashSet<String>(); final ZipOutputStream zipout = new ZipOutputStream(out); for (IFile f : container.getFiles()) { assertNoAbsolutePath(f); assertNoDuplicates(filenames, f); ZipEntry entry = new ZipEntry(f.getLocalPath()); entry.setTime(f.getLastModified()); if (f.getPermissions() != IFile.UNDEF_PERMISSIONS) { entry.setUnixMode(f.getPermissions()); } zipout.putNextEntry(entry); f.writeTo(zipout); zipout.closeEntry(); } zipout.finish(); }
Example 3
Source File: ZipUtil.java From APICloud-Studio with GNU General Public License v3.0 | 6 votes |
public static void zip(String path, List<File> files) throws IOException { ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream( new FileOutputStream(path), 1024)); for(File f : files) { String zipName = f.getName(); if(!f.getName().contains(".png")) { zipName = f.getName() + ".xml"; } ZipEntry ze = new ZipEntry(zipName); ze.setTime(f.lastModified()); DataInputStream dis = new DataInputStream(new BufferedInputStream( new FileInputStream(f))); zos.putNextEntry(ze); int c; while ((c = dis.read()) != -1) { zos.write(c); } } zos.setEncoding("gbk"); zos.closeEntry(); zos.close(); }
Example 4
Source File: FileUtils.java From frpMgr with MIT License | 5 votes |
/** * 将目录压缩到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 (int i = 0; i < files.length; i++) { if (files[i].isFile()) { // 如果是文件,则调用文件压缩方法 FileUtils .zipFilesToZipFile(dirPath, files[i], zouts); } else { // 如果是目录,则递归调用 FileUtils.zipDirectoryToZipFile(dirPath, files[i], zouts); } } } }
Example 5
Source File: FileHelper.java From bird-java with MIT License | 5 votes |
/** * 递归压缩方法 * * @param sourceFile 源文件 * @param zos zip输出流 * @param name 压缩后的名称 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * @throws Exception */ private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception { byte[] buf = new byte[BUFFER_SIZE]; if (sourceFile.isFile()) { // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字 zos.putNextEntry(new ZipEntry(name)); // copy文件到zip输出流中 int len; FileInputStream in = new FileInputStream(sourceFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); } // Complete the entry zos.closeEntry(); in.close(); } else { File[] listFiles = sourceFile.listFiles(); if (listFiles == null || listFiles.length == 0) { // 需要保留原来的文件结构时,需要对空文件夹进行处理 if (KeepDirStructure) { // 空文件夹的处理 zos.putNextEntry(new ZipEntry(name + "/")); // 没有文件,不需要文件的copy zos.closeEntry(); } } else { for (File file : listFiles) { // 判断是否需要保留原来的文件结构 if (KeepDirStructure) { // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠, // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了 compress(file, zos, name + "/" + file.getName(), KeepDirStructure); } else { compress(file, zos, file.getName(), KeepDirStructure); } } } } }
Example 6
Source File: FileUtils.java From Shop-for-JavaWeb with MIT License | 5 votes |
/** * 将目录压缩到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 (int i = 0; i < files.length; i++) { if (files[i].isFile()) { // 如果是文件,则调用文件压缩方法 FileUtils .zipFilesToZipFile(dirPath, files[i], zouts); } else { // 如果是目录,则递归调用 FileUtils.zipDirectoryToZipFile(dirPath, files[i], zouts); } } } }
Example 7
Source File: HttpDownHandler.java From AndroidWebServ with Apache License 2.0 | 5 votes |
/** 递归压缩文件进zip文件流 */ private void zip(ZipOutputStream zos, File file, String base) throws IOException { if (file.isDirectory()) { // 目录时 File[] files = file.listFiles(); if (null != files && files.length > 0) { for (File f : files) { zip(zos, f, base + "/" + f.getName()); // 递归 } } else { zos.putNextEntry(new ZipEntry(base + "/")); // 加入目录条目 zos.closeEntry(); } } else { zos.putNextEntry(new ZipEntry(base)); // 加入文件条目 FileInputStream fis = new FileInputStream(file); // 创建文件输入流 try { int count; // 读取计数 byte[] buffer = new byte[Config.BUFFER_LENGTH]; // 缓冲字节数组 /* 写入zip输出流 */ while ((count = fis.read(buffer)) != -1) { zos.write(buffer, 0, count); } } finally { zos.flush(); zos.closeEntry(); fis.close(); } } }
Example 8
Source File: MigrateForm.java From jeewx with Apache License 2.0 | 2 votes |
/** * 创建目录 * * @param zos * zip输出流 * @param relativePath * 相对路径 * @throws IOException */ private static void createZipNode(ZipOutputStream zos, String relativePath) throws IOException { ZipEntry zipEntry = new ZipEntry(relativePath); zos.putNextEntry(zipEntry); zos.closeEntry(); }
Example 9
Source File: MigrateForm.java From jeecg with Apache License 2.0 | 2 votes |
/** * 创建目录 * * @param zos * zip输出流 * @param relativePath * 相对路径 * @throws IOException */ private static void createZipNode(ZipOutputStream zos, String relativePath) throws IOException { ZipEntry zipEntry = new ZipEntry(relativePath); zos.putNextEntry(zipEntry); zos.closeEntry(); }