当前位置: 首页>>代码示例>>Java>>正文


Java ZipOutputStream.close方法代码示例

本文整理汇总了Java中org.apache.tools.zip.ZipOutputStream.close方法的典型用法代码示例。如果您正苦于以下问题:Java ZipOutputStream.close方法的具体用法?Java ZipOutputStream.close怎么用?Java ZipOutputStream.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.tools.zip.ZipOutputStream的用法示例。


在下文中一共展示了ZipOutputStream.close方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: zip

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * <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();
}
 
开发者ID:JoyLau,项目名称:joylau-springboot-daemon-windows,代码行数:28,代码来源:ZipUtils.java

示例2: zip

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
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();  
}
 
开发者ID:apicloudcom,项目名称:APICloud-Studio,代码行数:23,代码来源:ZipUtil.java

示例3: closeZout

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/** Close zout */
private void closeZout(final ZipOutputStream zOut, final boolean success)
    throws IOException {
    if (zOut == null) {
        return;
    }
    try {
        zOut.close();
    } catch (final IOException ex) {
        // If we're in this finally clause because of an
        // exception, we don't really care if there's an
        // exception when closing the stream. E.g. if it
        // throws "ZIP file must have at least one entry",
        // because an exception happened before we added
        // any files, then we must swallow this
        // exception. Otherwise, the error that's reported
        // will be the close() error, which is not the
        // real cause of the problem.
        if (success) {
            throw ex;
        }
    }
}
 
开发者ID:apache,项目名称:ant,代码行数:24,代码来源:Zip.java

示例4: downloadFiles

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * Downloads a collection of files within a directory.
 *
 * @param response The HTTP response.
 * @param status   The download status.
 * @param dir      The directory.
 * @param indexes  Only download files with these indexes within the directory.
 * @throws IOException If an I/O error occurs.
 */
private void downloadFiles(HttpServletResponse response, TransferStatus status, File dir, int[] indexes) throws IOException {
    String zipFileName = dir.getName() + ".zip";
    LOG.info("Starting to download '" + zipFileName + "' to " + status.getPlayer());
    status.setFile(dir);

    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''"+ encodeAsRFC5987(zipFileName));

    ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
    out.setMethod(ZipOutputStream.STORED);  // No compression.

    MediaFile parent = mediaFileService.getMediaFile(dir);
    List<MediaFile> allChildren = mediaFileService.getChildrenOf(parent, true, true, true);
    List<MediaFile> mediaFiles = new ArrayList<MediaFile>();
    for (int index : indexes) {
        mediaFiles.add(allChildren.get(index));
    }

    for (MediaFile mediaFile : mediaFiles) {
        zip(out, mediaFile.getParentFile(), mediaFile.getFile(), status, null);
    }

    out.close();
    LOG.info("Downloaded '" + zipFileName + "' to " + status.getPlayer());
}
 
开发者ID:FutureSonic,项目名称:FutureSonic-Server,代码行数:35,代码来源:DownloadController.java

示例5: zipFiles

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * 压缩文件或目录
 *
 * @param srcDirName   压缩的根目录
 * @param fileName     根目录下的待压缩的文件名或文件夹名,其中*或""表示跟目录下的全部文件
 * @param descFileName 目标zip文件
 */
public static void zipFiles(String srcDirName, String fileName, String descFileName) {
	// 判断目录是否存在
	if (srcDirName == null) {
		logger.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
		return;
	}
	File fileDir = new File(srcDirName);
	if (!fileDir.exists() || !fileDir.isDirectory()) {
		logger.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
		return;
	}
	String dirPath = fileDir.getAbsolutePath();
	File descFile = new File(descFileName);
	try {
		ZipOutputStream zouts = new ZipOutputStream(new FileOutputStream(descFile));
		if ("*".equals(fileName) || "".equals(fileName)) {
			FileUtils.zipDirectoryToZipFile(dirPath, fileDir, zouts);
		} else {
			File file = new File(fileDir, fileName);
			if (file.isFile()) {
				FileUtils.zipFilesToZipFile(dirPath, file, zouts);
			} else {
				FileUtils.zipDirectoryToZipFile(dirPath, file, zouts);
			}
		}
		zouts.close();
		logger.debug(descFileName + " 文件压缩成功!");
	} catch (Exception e) {
		logger.debug("文件压缩失败:" + e.getMessage());
		e.printStackTrace();
	}

}
 
开发者ID:funtl,项目名称:framework,代码行数:41,代码来源:FileUtils.java

示例6: zipFiles

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * 压缩文件或目录
 * @param srcDirName 压缩的根目录
 * @param fileName 根目录下的待压缩的文件名或文件夹名,其中*或""表示跟目录下的全部文件
 * @param descFileName 目标zip文件
 */
public static void zipFiles(String srcDirName, String fileName,
                            String descFileName) {
    // 判断目录是否存在
    if (srcDirName == null) {
        log.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
        return;
    }
    File fileDir = new File(srcDirName);
    if (!fileDir.exists() || !fileDir.isDirectory()) {
        log.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
        return;
    }
    String dirPath = fileDir.getAbsolutePath();
    File descFile = new File(descFileName);
    try {
        ZipOutputStream zouts = new ZipOutputStream(new FileOutputStream(descFile));
        if ("*".equals(fileName) || "".equals(fileName)) {
            FileHelper.zipDirectoryToZipFile(dirPath, fileDir, zouts);
        } else {
            File file = new File(fileDir, fileName);
            if (file.isFile()) {
                FileHelper.zipFilesToZipFile(dirPath, file, zouts);
            } else {
                FileHelper.zipDirectoryToZipFile(dirPath, file, zouts);
            }
        }
        zouts.close();
        log.debug(descFileName + " 文件压缩成功!");
    } catch (Exception e) {
        log.debug("文件压缩失败:" + e.getMessage());
        e.printStackTrace();
    }

}
 
开发者ID:liuxx001,项目名称:bird-java,代码行数:41,代码来源:FileHelper.java

示例7: zipFiles

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * 压缩文件或目录
 *
 * @param srcDirName   压缩的根目录
 * @param fileName     根目录下的待压缩的文件名或文件夹名,其中*或""表示跟目录下的全部文件
 * @param descFileName 目标zip文件
 */
public static void zipFiles(String srcDirName, String fileName, String descFileName) {
    // 判断目录是否存在
    if (srcDirName == null) {
        logger.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
        return;
    }
    File fileDir = new File(srcDirName);
    if (!fileDir.exists() || !fileDir.isDirectory()) {
        logger.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
        return;
    }
    String dirPath = fileDir.getAbsolutePath();
    File descFile = new File(descFileName);
    try {
        ZipOutputStream zouts = new ZipOutputStream(new FileOutputStream(descFile));
        if ("*".equals(fileName) || "".equals(fileName)) {
            FileUtils.zipDirectoryToZipFile(dirPath, fileDir, zouts);
        } else {
            File file = new File(fileDir, fileName);
            if (file.isFile()) {
                FileUtils.zipFilesToZipFile(dirPath, file, zouts);
            } else {
                FileUtils.zipDirectoryToZipFile(dirPath, file, zouts);
            }
        }
        zouts.close();
        logger.debug(descFileName + " 文件压缩成功!");
    } catch (Exception e) {
        logger.debug("文件压缩失败:" + e.getMessage());
        e.printStackTrace();
    }

}
 
开发者ID:ansafari,项目名称:melon,代码行数:41,代码来源:FileUtils.java

示例8: zipFiles

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * 压缩文件或目录
 * @param srcDirName 压缩的根目录
 * @param fileName 根目录下的待压缩的文件名或文件夹名,其中*或""表示跟目录下的全部文件
 * @param descFileName 目标zip文件
 */
public static void zipFiles(String srcDirName, String fileName,
		String descFileName) {
	// 判断目录是否存在
	if (srcDirName == null) {
		log.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
		return;
	}
	File fileDir = new File(srcDirName);
	if (!fileDir.exists() || !fileDir.isDirectory()) {
		log.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
		return;
	}
	String dirPath = fileDir.getAbsolutePath();
	File descFile = new File(descFileName);
	try {
		ZipOutputStream zouts = new ZipOutputStream(new FileOutputStream(
				descFile));
		if ("*".equals(fileName) || "".equals(fileName)) {
			FileUtils.zipDirectoryToZipFile(dirPath, fileDir, zouts);
		} else {
			File file = new File(fileDir, fileName);
			if (file.isFile()) {
				FileUtils.zipFilesToZipFile(dirPath, file, zouts);
			} else {
				FileUtils
						.zipDirectoryToZipFile(dirPath, file, zouts);
			}
		}
		zouts.close();
		log.debug(descFileName + " 文件压缩成功!");
	} catch (Exception e) {
		log.debug("文件压缩失败:" + e.getMessage());
		e.printStackTrace();
	}

}
 
开发者ID:EleTeam,项目名称:Shop-for-JavaWeb,代码行数:43,代码来源:FileUtils.java

示例9: Zipper

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * 创建Zipper对象
 * 
 * @param out
 *            输出流
 * @param filter
 *            文件过滤,不过滤可以为null。
 * @param srcFilename
 *            源文件名。可以有多个源文件,如果源文件是目录,那么所有子目录都将被包含。
 */
protected Zipper(OutputStream out, List<FileEntry> fileEntrys,
		String encoding) {
	Assert.notEmpty(fileEntrys);
	long begin = System.currentTimeMillis();
	log.debug("开始制作压缩包");
	try {
		try {
			zipOut = new ZipOutputStream(out);
			if (!StringUtils.isBlank(encoding)) {
				log.debug("using encoding: {}", encoding);
				zipOut.setEncoding(encoding);
			} else {
				log.debug("using default encoding");
			}
			for (FileEntry fe : fileEntrys) {
				zip(fe.getFile(), fe.getFilter(), fe.getZipEntry(), fe
						.getPrefix());
			}
		} finally {
			zipOut.close();
		}
	} catch (IOException e) {
		throw new RuntimeException("制作压缩包时,出现IO异常!", e);
	}
	long end = System.currentTimeMillis();
	log.info("制作压缩包成功。耗时:{}ms。", end - begin);
}
 
开发者ID:huanzhou,项目名称:jeecms6,代码行数:38,代码来源:Zipper.java

示例10: downloadDirectory

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * Downloads all files in a directory (including sub-directories). The files are packed together in an
 * uncompressed zip-file.
 *
 * @param response The HTTP response.
 * @param status   The download status.
 * @param file     The file to download.
 * @param range    The byte range, may be <code>null</code>.
 * @throws IOException If an I/O error occurs.
 */
private void downloadDirectory(HttpServletResponse response, TransferStatus status, File file, LongRange range) throws IOException {
    String zipFileName = file.getName() + ".zip";
    LOG.info("Starting to download '" + zipFileName + "' to " + status.getPlayer());
    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''"+ encodeAsRFC5987(zipFileName));

    ZipOutputStream out = new ZipOutputStream(RangeOutputStream.wrap(response.getOutputStream(), range));
    out.setMethod(ZipOutputStream.STORED);  // No compression.

    zip(out, file.getParentFile(), file, status, range);
    out.close();
    LOG.info("Downloaded '" + zipFileName + "' to " + status.getPlayer());
}
 
开发者ID:FutureSonic,项目名称:FutureSonic-Server,代码行数:24,代码来源:DownloadController.java

示例11: zip

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
public Jile zip(String zipFileName) throws Exception {
    File filex = new File(zipFileName);
    if (!filex.exists()) {
        if (!filex.getParentFile().exists()) {
            filex.getParentFile().mkdirs();
        }
        filex.createNewFile();
    }
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(filex));
    if (this.file.isDirectory()) {
        zip(out, this.file, "");
        out.close();
    } else {
        FileInputStream fis = new FileInputStream(this.file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] buf = new byte[1024];
        int len;
        FileOutputStream fos = new FileOutputStream(zipFileName);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ZipOutputStream zos = new ZipOutputStream(bos);
        ZipEntry ze = new ZipEntry(this.file.getName());
        zos.putNextEntry(ze);
        while ((len = bis.read(buf)) != -1) {
            zos.write(buf, 0, len);
            zos.flush();
        }
        bis.close();
        zos.close();
    }
    return this;
}
 
开发者ID:hou80houzhu,项目名称:rocserver,代码行数:32,代码来源:Jile.java

示例12: zip

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * zip方法对文件或者文件夹进行压缩
 * 
 * @param zipstringPath
 *            为要压缩的文件或者文件夹路径
 * @param zipFileName
 *            为压缩之后生成的文件名
 */
public void zip(String zipstringPath, String zipFileName) throws Exception {
	try {

		byte[] buf = new byte[1024];
		File zipPath = new File(zipstringPath);
		int filelen = zipPath.listFiles().length;
		String[] filenames = new String[filelen];
		try {
			File[] files = zipPath.listFiles();
			for (int i = 0; i < filelen; i++) {
				filenames[i] = zipPath.getPath() + File.separator
						+ files[i].getName();
			}
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
					zipFileName));
			for (int i = 0; i < filenames.length; i++) {
				FileInputStream in = new FileInputStream(filenames[i]);
				out.putNextEntry(new ZipEntry(files[i].getName()));
				int len;
				while ((len = in.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				out.closeEntry();
				in.close();
			}

			out.close();
		} catch (IOException e) {
			System.out.println(e);
		}

	} catch (Exception ex) {
		ex.printStackTrace(System.out);

	}
}
 
开发者ID:easyjf,项目名称:easyjweb,代码行数:45,代码来源:ZipUtils.java

示例13: zip

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * 压缩文件
 * 
 * @param out
 *            OutputStream 输出流
 * @param entryNameList
 *            文件别名列表
 * @param fileList
 *            文件列表
 * @param encoding
 *            zip的实体名称和注释编码,推荐使用GBK
 * @param comment
 *            zip文件的注释
 * @param useSameDir
 *            值为 false 时,保存原有目录结构,fileListMap 中的文件别名 key 将被忽略; true
 *            的时候fileList 中的文件夹将被被忽略
 * @return 是否压缩成功
 */
public static Boolean zip(OutputStream out, List<String> entryNameList, List<String> fileList,
        String encoding, String comment, Boolean useSameDir) {
    Boolean isSuccess = false;
    try {
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(out);
        setZipOutputStream(zos, encoding, comment);
        // Compress the files
        for (int i = 0; i < fileList.size(); i++) {
            try {
                String entryName = null;
                if (null != entryNameList && entryNameList.size() > 0) {
                    entryName = entryNameList.get(i);
                }

                addEntry(entryName, new File(fileList.get(i)), zos, useSameDir);
            } catch (Throwable e1) {
                logger.error("add file " + fileList.get(i) + " to zip file failed: "
                        + ExceptionUtils.getRootCauseMessage(e1));
            }
        }
        zos.flush();
        // Complete the ZIP file
        zos.close();
        logger.debug("ZIP file content wireted to OutputStream successed.");
        isSuccess = true;
    } catch (IOException ex) {
        isSuccess = false;
        logger.error("failed to zip: ", ex);
    }

    return isSuccess;
}
 
开发者ID:jiucai,项目名称:appframework,代码行数:52,代码来源:ZipFileUtil.java

示例14: zipFiles

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * 压缩文件或目录
 * @param srcDirName 压缩的根目录
 * @param fileName 根目录下的待压缩的文件名或文件夹名,其中*或""表示跟目录下的全部文件
 * @param descFileName 目标zip文件
 */
public static void zipFiles(String srcDirName, String fileName,
		String descFileName) {
	// 判断目录是否存在
	if (srcDirName == null) {
		log.debug("文件压缩失败,目录" + srcDirName + "不存在!");
		return;
	}
	File fileDir = new File(srcDirName);
	if (!fileDir.exists() || !fileDir.isDirectory()) {
		log.debug("文件压缩失败,目录" + srcDirName + "不存在!");
		return;
	}
	String dirPath = fileDir.getAbsolutePath();
	File descFile = new File(descFileName);
	try {
		ZipOutputStream zouts = new ZipOutputStream(new FileOutputStream(
				descFile));
		if ("*".equals(fileName) || "".equals(fileName)) {
			FileUtils.zipDirectoryToZipFile(dirPath, fileDir, zouts);
		} else {
			File file = new File(fileDir, fileName);
			if (file.isFile()) {
				FileUtils.zipFilesToZipFile(dirPath, file, zouts);
			} else {
				FileUtils
						.zipDirectoryToZipFile(dirPath, file, zouts);
			}
		}
		zouts.close();
		log.debug(descFileName + "文件压缩成功!");
	} catch (Exception e) {
		log.debug("文件压缩失败:" + e.getMessage());
		e.printStackTrace();
	}

}
 
开发者ID:cncduLee,项目名称:bbks,代码行数:43,代码来源:FileUtils.java


注:本文中的org.apache.tools.zip.ZipOutputStream.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。