IO Foundation 6-解压缩zip文件
作者:佚名
上传时间:2019-05-28
版权申诉
需求:
这一步也非常简单,主要是demo下ZipEntry的用法,就是把一个以zip扩展名结尾的文件进行解压缩。
所以我就直接贴上代码了:
实现:
- /**
- * This class will handle the folder zipper related operations
- *
- * @author cwang58
- * @created date: Aug 4, 2012
- */
- public class FolderZipper implements IFolderZipper {
- private static Logger logger = LoggerFactory.getLogger(FolderZipper. class );
- /**
- * to unzip a zip file ,it will read all the entries in the zip file if the
- * entry is a folder ,then it will created a folder structure (including the
- * parent folder structure) if the entry is a file ,then it use file output
- * stream to output this file
- *
- * @param zipFileName
- * the zip file name
- * @param destPath
- * the path that we want to zip to
- */
- public void unzip(String zipFileName, String destPath) throws Exception {
- // verify the parameters
- if (zipFileName == null || zipFileName.trim().equals(EMPTY_STRING)) {
- if (logger.isErrorEnabled()) {
- logger.error( "zipFileName parameter can't be null" );
- }
- throw new ParameterNullException(ZIP_UNZIP_ERROR);
- }
- if (destPath == null ) {
- if (logger.isErrorEnabled()) {
- logger.error( "destPath parameter can't be null" );
- }
- throw new ParameterNullException( "ZIP_UNZIP_ERROR" );
- }
- // verify the zip file 's file extension (only support ZIP)
- if (!zipFileName.trim().endsWith(ZIP_EXTENSION)) {
- if (logger.isErrorEnabled()) {
- logger.error( "the zip file MUST BE end with zip suffix" );
- }
- throw new ParameterInvalidException(ZIP_UNZIP_ERROR);
- }
- try {
- FileInputStream fis = new FileInputStream(zipFileName);
- ZipInputStream zis = new ZipInputStream(fis);
- ZipEntry ze = null ;
- if (destPath.equals(DOT))
- destPath = EMPTY_STRING;
- while ((ze = zis.getNextEntry()) != null ) {
- // if the zipEntry is a folder
- // then must create the folder structure
- if (ze.isDirectory()) {
- if (logger.isDebugEnabled()) {
- logger.debug( "Processing folder:" + ze.getName());
- }
- // construct the dirName by string concating the destpath
- // and the foldername
- String dirName = destPath + ze.getName();
- // creating the folder structure
- File f = new File(dirName);
- f.mkdirs();
- if (logger.isDebugEnabled()) {
- logger.debug( "The folder " + f.getAbsolutePath()
- + " is created" );
- }
- }
- // if the zipEntry file is a file
- // then use a FileOutputStream to write this file
- else {
- if (logger.isDebugEnabled()) {
- logger.debug( "Processing file: " + ze.getName());
- }
- // construct the fileName by string concating the destpath
- // and the foldername
- String fileName = destPath + ze.getName();
- File f = new File(fileName);
- // open a FileOutputStream to write this file
- FileOutputStream fos = new FileOutputStream(f);
- int tmp = - 1 ;
- while ((tmp = zis.read()) != - 1 ) {
- fos.write(tmp);
- }
- zis.closeEntry();
- fos.close();
- if (logger.isDebugEnabled()) {
- logger.debug( "The file " + f.getAbsolutePath()
- + " is created" );
- }
- }
- }
- zis.close();
- } catch (FileNotFoundException ex) {
- if (logger.isErrorEnabled()) {
- logger.error( "file not found" );
- }
- throw new InvalidFolderZipperException(ZIP_UNZIP_ERROR, ex);
- } catch (IOException ex) {
- if (logger.isErrorEnabled()) {
- logger.error( "io exception occured" );
- }
- throw new InvalidFolderZipperException(ZIP_UNZIP_ERROR,
- ex);
- }
- }
- }
(1)在47-93行还是对入参进行严格的检查。
(2)第103行打开了一个ZipInputStream,然后在第115行对于这个zip文件里的所有的条目进行遍历。
(3)第119-159行对于条目是一个目录的情况进行了处理,它必须去相应的创建目录结构
(4)第163-215行对于条目是个文件的情况进行了处理,它打开一个文件输出流然后写这个Zip的文件条目到目标目录下相应的位置。
本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/980320,如需转载请自行联系原作者
免责申明:文章和图片全部来源于公开网络,如有侵权,请通知删除 server@dude6.com
用户评论
相关推荐
IO Foundation 6-解压缩zip文件
需求: 这一步也非常简单,主要是demo下ZipEntry的用法,就是把一个以zip扩展名结尾的文件进行解压缩。 所
Java IO流Zip文件的压缩和解压缩
NULL 博文链接:https://beijishiqidu.iteye.com/blog/1944225
ZIP
3KB
2020-08-11 11:49
zip压缩解压缩文件
zip压缩/解压缩文件
RAR
0B
2019-09-10 09:30
压缩与解压缩zip文件
关于压缩zip文件,和解压缩的zip文件的代码,掉用的时候必须引入ICSharpCode.SharpZipLib.dll
CS
0B
2019-09-20 02:35
解压缩zip文件控件
解压缩zip文件所需要的控件,可以完美将zip文件解压出来
DLL
0B
2019-03-13 06:57
java解压缩zip文件
zip格式是一种免费的压缩文件格式,我们可以通过java类库编写函数来解压缩它,实现winwar这类软件的功能。
JAVA
0B
2018-12-26 07:34
java文件zip解压缩
java 文件zip解压缩,单个文件以及多个文件,有需要的请下载。
JAVA
0B
2018-12-26 07:34
Java Zip文件解压缩
为了解压缩zip都折腾两天了,查看了许多谷歌、百度来的code, 真实无语了,绝大多数是不能用的。这可能跟我的开发环境有关吧。 我用的是Ubuntu14.04,eclipse 用的是STS3.
PDF
38KB
2020-12-23 05:51
文件压缩解压缩
文件压缩解压缩(rar)
DOC
0B
2019-01-07 09:47
文件压缩解压缩
简单的一个工具类,支持zip、rar、tar等的压缩解压缩,亲自测过可用
ZIP
0B
2019-05-31 23:47