当前位置:首页 > 开发 > 编程语言 > Java > 正文

Java SE: How to Decompress File (.zip)

发表于: 2014-07-11   作者:DavyJones2010   来源:转载   浏览次数:
摘要: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.log4j.Logger;

public class FileDeCompressUtil
{
    private static final Logger logger = Logger
            .getLogger(FileDeCompressUtil.class);

    private static final int BUFFER_SIZE = 2048;

    private FileDeCompressUtil()
    {
    }

    public static List<String> decompressFileAndDeleteOrig(File zippedFile,
            String destPath) throws IOException
    {
        List<String> decompressFileNameList = decompressFile(zippedFile,
                destPath);

        if (zippedFile.exists())
        {
            zippedFile.delete();
        }
        else
        {
            logger.error(String.format(
                    "zippedFile: [%s] doesn't exists, thus failed delete",
                    zippedFile.getName()));
        }
        return decompressFileNameList;
    }

    public static List<String> decompressFile(File zippedFile, String destPath)
            throws IOException
    {
        InputStream is = new FileInputStream(zippedFile);
        List<String> decompressFileNameList = decompressFile(is, destPath);
        is.close();

        return decompressFileNameList;
    }

    public static List<String> decompressFile(InputStream is, String destPath)
            throws IOException
    {
        List<String> decompressFileNameList = new ArrayList<String>();

        byte[] buffer = new byte[BUFFER_SIZE];
        logger.info(String.format("Start decompressFile, destPath: [%s]",
                destPath));

        File folder = new File(destPath);
        if (!folder.exists())
        {
            logger.warn(String.format(
                    "destPath: [%s] doesn't exits, thus create dir for it",
                    destPath));
            folder.mkdirs();
        }
        ZipInputStream zis = new ZipInputStream(is);

        ZipEntry ze = zis.getNextEntry();

        while (ze != null)
        {
            String fileName = ze.getName();
            String unzippedFileNameWithPath = destPath + File.separator
                    + fileName;
            File newFile = new File(unzippedFileNameWithPath);
            File parentDir = new File(newFile.getParent());

            logger.info(String.format("decompressed file: [%s]",
                    unzippedFileNameWithPath));

            decompressFileNameList.add(unzippedFileNameWithPath);

            if (!parentDir.exists())
            {
                logger.warn(String
                        .format("parentDir: [%s] doesn't exits, thus create dir for it",
                                parentDir.getAbsolutePath()));

                parentDir.mkdirs();
            }

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0)
            {
                fos.write(buffer, 0, len);
            }

            fos.close();
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

        logger.info(String.format(
                "Finished decompressFile. decompressFileNameList: [%s]",
                decompressFileNameList));

        return decompressFileNameList;
    }
}

 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.junit.Test;

public class FileDeCompressUtilTest
{
    @Test
    public void decompressFileTest() throws FileNotFoundException, IOException
    {
        File file = new File("src/test/resources/test.zip");

        String destPath = "src/test/resources";

        List<String> decompressFileNameList = FileDeCompressUtil
                .decompressFileAndDeleteOrig(file, destPath);

        for (String decompressFileName : decompressFileNameList)
        {
            new File(decompressFileName).delete();
        }
    }
}

 

 

Reference Links:

1) http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/

Java SE: How to Decompress File (.zip)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Java API中有很多都使用了SecurityManager,这到底是什么玩意?最近看公司的产品的源码,也有不少Se
静态导入 l JDK 1.5 增加的静态导入语法用于导入指定类的某个静态属性值(方法)或全部静态属性值(方
昨天晚上开始学习传智播客的JAVA SE基础视频,到今天晚上已经把基础视频1-5看完了,因为之前段段续
1.知识体系图 java技术的主要分支方向: J2SE:java的标准版,提供基本的底层支持,图形界面,05年java十
1.HashSet底层使用HashMap实现的 当使用add方法将对象添加到Set当中时,实际上是将该对象作为底层所
目录结构如下: import java.io.File; import java.io.FileInputStream; import java.io.FileOutput
Java的集合框架分为两个系列,Collection和Map系列。在大学期间,学习数据结构时,好像学习了线性表
Java Logging 用惯了log4j等日志工具,竟然不知Java还自带了个log工具。今天有空了就来了解一下。
In order to make VS can edit readonly file, you need to make sure below two options in VS->
原文地址:https://www.digitalocean.com/community/articles/how-to-edit-the-sudoers-file-on-ubu
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号