Java Code Examples for org.apache.tools.zip.ZipOutputStream#close()

The following examples show how to use org.apache.tools.zip.ZipOutputStream#close() . 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 vote down vote up
/**
 * <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: ZipUtil.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
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 3
Source File: FileUtils.java    From frpMgr with MIT License 5 votes vote down vote up
/**
 * 压缩文件或目录
 * @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();
	}

}
 
Example 4
Source File: FileUtils.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
/**
 * 压缩文件或目录
 * @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();
	}

}
 
Example 5
Source File: MigrateForm.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 压缩
 * 
 * @param zipFileName
 *            压缩产生的zip包文件名--带路径,如果为null或空则默认按文件名生产压缩文件名
 * @param relativePath
 *            相对路径,默认为空
 * @param directory
 *            文件或目录的绝对路径
 * @throws FileNotFoundException
 * @throws IOException
 */
public static String zip(String zipFileName, String relativePath, String directory) throws FileNotFoundException, IOException {
	String fileName = zipFileName;
	if (fileName == null || fileName.trim().equals("")) {
		File temp = new File(directory);
		if (temp.isDirectory()) {
			fileName = directory + ".zip";
		} else {
			if (directory.indexOf(".") > 0) {
				fileName = directory.substring(0, directory.lastIndexOf(".")) + ".zip";
			} else {
				fileName = directory + ".zip";
			}
		}
	}
	ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(fileName));
	try {
		zip(zos, relativePath, directory);
	} catch (IOException ex) {
		throw ex;
	} finally {
		if (null != zos) {
			zos.close();
		}
	}
	return fileName;
}
 
Example 6
Source File: Zipper.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 创建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);
}
 
Example 7
Source File: MigrateForm.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 压缩
 * 
 * @param zipFileName
 *            压缩产生的zip包文件名--带路径,如果为null或空则默认按文件名生产压缩文件名
 * @param relativePath
 *            相对路径,默认为空
 * @param directory
 *            文件或目录的绝对路径
 * @throws FileNotFoundException
 * @throws IOException
 */
public static String zip(String zipFileName, String relativePath, String directory) throws FileNotFoundException, IOException {
	String fileName = zipFileName;
	if (fileName == null || fileName.trim().equals("")) {
		File temp = new File(directory);
		if (temp.isDirectory()) {
			fileName = directory + ".zip";
		} else {
			if (directory.indexOf(".") > 0) {
				fileName = directory.substring(0, directory.lastIndexOf(".")) + ".zip";
			} else {
				fileName = directory + ".zip";
			}
		}
	}
	ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(fileName));
	try {
		zip(zos, relativePath, directory);
	} catch (IOException ex) {
		throw ex;
	} finally {
		if (null != zos) {
			zos.close();
		}
	}
	return fileName;
}