`

Java SE: How to Decompress File (.zip)

阅读更多
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/

0
0
分享到:
评论
3 楼 DavyJones2010 2014-07-15  
knightdf 写道
freezingsky 写道
这年头代码和内容都是copy来copy去的,至少讲一讲过程分析或者是原理实现吧。

哈哈,就是啊,一个压缩解压缩的代码..只丢个代码在上面,估计久了他自己也忘了

The intent of this article is just introducing the usage of zip/unzip AIP in java.util.zip package, and it acts as a personal code base for handy future usage. If you guys are interested in each step in detail, please refer to "http://www.oracle.com/technetwork/articles/java/compress-1565076.html". Sorry for any inconvenience.
2 楼 knightdf 2014-07-12  
freezingsky 写道
这年头代码和内容都是copy来copy去的,至少讲一讲过程分析或者是原理实现吧。

哈哈,就是啊,一个压缩解压缩的代码..只丢个代码在上面,估计久了他自己也忘了
1 楼 freezingsky 2014-07-11  
这年头代码和内容都是copy来copy去的,至少讲一讲过程分析或者是原理实现吧。

相关推荐

    (JAVA)利用Java实现zip压缩.解压缩.rar_decompress rar java_zip 压缩

    在Java编程语言中,我们可以利用内置的`java.util.zip`包来实现文件的压缩和解压缩功能。这个包提供了一系列的类,如`ZipOutputStream`用于创建ZIP压缩文件,`ZipInputStream`则用于读取和解压缩ZIP文件。在本教程中...

    Java 项目-java的JAVA文件压缩与解压缩实践.zip

    本项目“Java 项目-java的JAVA文件压缩与解压缩实践.zip”提供了一个实战示例,帮助开发者了解如何在Java中实现这一功能。 首先,我们需要介绍的是Java中的`java.util.zip`包,这个包提供了对ZIP文件格式的支持。在...

    java zip 压缩 解压缩 附带ant.jar

    `java.util.zip`是Java标准库提供的用于处理ZIP文件的API,它包含了处理压缩和解压缩功能的基本类。在本主题中,我们将深入探讨如何使用Java来对文件进行zip压缩和解压缩,并提及`ant.jar`的作用。 首先,让我们来...

    java源码:Java用GZIP压缩解压文件.rar

    public static void decompressFile(String inputFile, String outputFile) throws IOException { GZIPInputStream gzipIn = new GZIPInputStream(new FileInputStream(inputFile)); FileOutputStream fileOut = ...

    java语言,解压缩文件夹.zip

    Java语言在处理文件压缩与解压缩任务时,通常会使用内置的`java.util.zip`库。这个库提供了对ZIP和GZIP格式的支持,使得我们能够方便地读取、创建和修改压缩文件。在这个"java语言,解压缩文件夹.zip"的压缩包中,...

    Java用GZIP压缩解压文件.zip

    public static void decompressFile(String inputFile, String outputFile) throws IOException { try (FileInputStream fis = new FileInputStream(inputFile); GZIPInputStream gis = new GZIPInputStream(fis)...

    Delphi实现Zip压缩/解压缩

    ZipService.DecompressFile('MyArchive.zip', 'DestinationFile.txt'); end; ``` 在这个例子中,`CreateOleObject`函数用于实例化在DLL中注册的COM对象,然后就可以调用其`CompressFile`和`DecompressFile`方法了...

    tools for mapplestory files and map decompress.zip

    为此,专门的工具应运而生,如"tools for mapplestory files and map decompress.zip"中的WZ-Explorer-Rebuild。本文将深入探讨这一工具及其在MapleStory文件处理和地图解压缩中的应用。 WZ-Explorer-Rebuild是一款...

    decompress_java_

    在Java编程语言中,`java.util.zip`包提供了一系列的类和接口,使得开发者能够方便地处理各种压缩格式,特别是ZIP格式。本文将详细讲解如何使用这些类来实现ZIP文件的压缩与解压缩功能。 首先,我们要理解ZIP文件...

    Python应用实战案例:Python压缩NC格式(附代码).zip

    def compress_to_zip(input_file, output_zip): with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf: zipf.write(input_file) compress_to_zip('data.nc', 'data.nc.zip') ``` 在这个例子中...

    junrar.jar.zip

    Files.copy(in, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); in.close(); } } rarFile.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 这段代码首先创建了一个`...

    au3反编译源码

    LZSS.exe Called after to decryption to decompress the script ExtractExeIcon.exe Used to extract the MainIcon(s) from the ScriptExe Doc\ Additional document about decompiling related stuff Tidy\ ...

    Java中文件的压缩处理-Java教程共2页.pdf.zi

    Java提供了多种库来支持这项功能,其中Java PDFBox是一个专注于处理PDF文档的库,而与文件压缩相关的库则有Java的内置API `java.util.zip` 和第三方库如Apache Commons Compress。 首先,让我们讨论Java的内置压缩...

    java zip解压缩

    Java Zip压缩和解压缩是Java开发中常见的文件处理任务,主要涉及到`java.util.zip`包中的类和方法。本文将详细讲解如何使用Java进行Zip文件的压缩与解压缩操作,并结合给定的标签"源码"和"工具"来探讨实际应用场景。...

    java ftp 上传 下载 rar 压缩 解压

    本篇将详细介绍如何使用Java实现FTP(File Transfer Protocol)传输以及RAR文件的处理。 首先,Java FTP上传和下载主要依赖于`java.net`和`java.io`包中的类。`java.net.Socket`用于建立FTP连接,`java.io`用于输入...

    quicklz1.5.0.zip

    2. **stream_compress.c** 和 **stream_decompress.c**:这两个文件提供了流式压缩和解压缩的功能。流式处理允许数据在内存或磁盘流中连续处理,而不需要一次性加载整个文件到内存,这对于处理大文件或实时数据流...

    lz4-java-1.6.0.zip

    标题中的“lz4-java-1.6.0.zip”指的是一个使用Java编写的LZ4压缩库的版本1.6.0的压缩包文件。LZ4是一种快速的数据压缩算法,广泛应用于各种软件和系统中,以提高数据传输和存储效率。这个版本是针对Java平台的实现...

    7Zip lzma LZ4 fastlz and Zip Multiplatform Plugin v2.7.5

    This is a shared library for Android, iOS, OSX, Windows, Linux and webGL to decompress 7z (7zip) files and to compress/decompress zip/gzip (.zip/.gz), LZ4 (.lz4), brotli (.br), fastLZ files and ...

    山东科技大学算法设计与分析实验5:实现哈夫曼编码 源.cpp+报告

    实验五 实现哈夫曼编码.cpp`会包含实现这些步骤的函数,例如,可能有`buildHuffmanTree`用于构建树,`generateCodes`用于生成编码,以及`compress`和`decompress`用于压缩和解压缩数据。通过分析这些函数,我们可以...

    ZipTV.v6.8.4.Full.Source.For.Delphi

    站里以前那个 6.8.4 是编译不了的... Click: OK - Click: Install The following four tabs will be added to your component palette: ZTV Compress ZTV Decompress ZTV Tools ZTV Volumes - Close the windowEnjoy :)

Global site tag (gtag.js) - Google Analytics