Java decompress zip package

2010-10-13  来源:本站原创  分类:Java  人气:130 

package test;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Unzip
{
    public static void main(String[] args)
    {
        String zipFilePath = "D:\\zip\\csv.zip";
        File zipFile = new File(zipFilePath);
        String targetDirectoryPath = null;
        //  When the target directory is empty when you extract the files to the directory where the file is  .
        String targetDirectory = targetDirectoryPath;
        ZipInputStream zis = null;
        if (targetDirectoryPath == null)
        {
            targetDirectory = zipFile.getParent() + System.getProperty("file.separator");
        }

        try
        {
            zis = new ZipInputStream(new FileInputStream(zipFilePath));
            ZipEntry entry;

            //  Create a folder after extraction  .
            while ((entry = zis.getNextEntry()) != null)
            {
                if (!entry.isDirectory())
                {
                    continue;
                }
                File directory = new File(targetDirectory, entry.getName());
                if (!directory.exists())
                {
                    if (!directory.mkdirs())
                    {
                        System.exit(0);
                    }
                    zis.closeEntry();
                }

            }
            zis.close();

            //  Unzip the file  .
            zis = new ZipInputStream(new FileInputStream(zipFilePath));
            while (((entry = zis.getNextEntry()) != null))
            {
                if (entry.isDirectory())
                {
                    continue;
                }
                File unzippedFile = new File(targetDirectory, entry.getName());

                String filePath = unzippedFile.getPath();
                if (filePath.endsWith("."))
                {
                    String newPath = filePath.substring(0, filePath.length() - 1);
                    File dir = new File(newPath);
                    if (!dir.exists())
                    {
                        dir.mkdir();
                    }
                    continue;
                }

                if (!unzippedFile.exists())
                {
                    unzippedFile.createNewFile();
                }
                FileOutputStream fout = new FileOutputStream(unzippedFile);
                DataOutputStream dout = new DataOutputStream(fout);

                byte[] b = new byte[1024];
                int len = 0;
                while ((len = zis.read(b)) != -1)
                {
                    dout.write(b, 0, len);
                }
                dout.close();
                fout.close();
                zis.closeEntry();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (zis != null)
                {
                    zis.close();
                }

                //zipFile.delete();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}

Section directly to the source code.

相关文章
  • Java decompress zip package 2010-10-13

    package test; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Unzip {

  • JAVA operating ZIP file collection 2010-09-26

    JAVA operating ZIP file collection Article Transfer from: http://www.abcjava.com/html/JAVA/JAVAjichu/2009/1018/1080.html Multiple files in the network transmission time, if a transfer is often a very time-consuming, but also consume more computer sys

  • java-compression and decompression of the hierarchical structure --- package java.util.zip 2009-11-12

    Package hierarchical structure: All Packages Class Hierarchy java.lang. Object java.util.zip. Adler32 (implements java.util.zip. Checksum) java.util.zip. CRC32 (implements java.util.zip. Checksum) java.util.zip. Deflater java.util.zip. Inflater java.

  • zip package java quickly read a stream of bytes in the specified file 2010-12-11

    Zip package quickly read into the string, there is another one way. However, a large zip bag on super slow read speed. This method only for as long as you identify a file in the zip package, then would not read more than 1 second, Enumeration read th

  • Android in the java .* and javax .* package 2010-07-15

    Following a split, as I wrote in Java for Android uses a lot of java .* and javax .* packages. In the end, how many do, if you are interested can download the source code for Android. Xiaofeng's blog in 2007 to see his article published , he found th

  • java create zip archive 2010-10-19

    package javaio; import java.io.*; import java.util.zip.*; public class ZipCompress{ public static void main(String args[]){ try{ ZipCompress zip=new ZipCompress(); FileOutputStream fileOut=new FileOutputStream("Example.zip"); CheckedOutputStream

  • java压缩zip文件中文乱码问题解决方法 2014-12-10

    这篇文章主要介绍了java压缩zip文件中文乱码问题的解决方法,需要的朋友可以参考下 通常用java来打包文件生成压缩文件后,有如下两个地方会出现乱码 : 1.内容的中文乱码问题,这个问题网上很多人给出了解决方法,主要有两种方法:一是修改sun的源码:另一个是使用开源的类库org.apache.tools.zip.ZipOutputStream和org.apache.tools.zip.ZipEntry,这两个类ant.jar中有,可以直接下载使用即可,毫无疑问,选择后者更方便 2.压缩文件注释

  • Java编写zip压缩和解压 2014-10-24

    ZipCompressor package com.liam.Utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.uti

  • java generate zip file 2010-04-29

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipOutputStreamDemo ( public static void main (String [] args) throws Exception (

  • Install Ruby 1.9 windows binary zip package 2010-02-21

    This article has been lagging behind, because the official has a new ruby 1.9 installer to install Ruby 1.9zip If you experience problems to the windows, please install a key installation package as follows: Release Candidate 2 2010-02-06 22:00 ruby-

  • JAVA extracting zip file 2010-10-23

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** * @author zhaoliangyuan * @E-mail [email protected] * @dateTi

  • Use InstallAnywhere7.1 production Java exe installation package (rpm) 2011-08-19

    Use InstallAnywhere7.1 production Java exe installation package Java application development and testing project is completed, companies need to archive this package (ie jar), and made into a commercial installation. 1, in the production into a jar f

  • 利用Java实现zip压缩/解压缩 2014-07-30

    由于网络带宽有限,所以数据文件的压缩有利于数据在Internet上的快速传输,同时也节 省服务器的外存空间. Java 1.1实现了I/O数据流与网络数据流的单一接口,因此数据的压缩.网络传输和解 压缩的实现比较容易,下面介绍利用ZipEntry.ZipInputStream和ZipOutputStream三个Java 类实现zip数据压缩方式的编程方法. zip压缩文件结构:一个zip文件由多个entry组成,每个entry有一个唯一的名称,entry的 数据项存储压缩数据. 与zip文件有关

  • 使用java.util.zip压缩.解压文件 2012-09-04

    压缩文件: import java.util.zip.*; import java.io.*; public class ZipTest { public static void main(String[] args) { if (args.length < 1) { System.out.println("请输入参数:javac ZipTest 需压缩的文件名及路径 保存的文件名及路径.(e.g. c:/test.txt)"); } else { ZipCompressor c

  • (转)java.util.zip.ZipException 2013-07-08

    发布应用的时候,发现生产环境报将编译环境编译好的WAR包拿到生产环境,报异常如下: [java] view plain copy Caused by: java.util.zip.ZipException: error in opening zip file at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:127) at java.util.jar.JarFile

  • Java实现Zip压缩解压缩 2013-11-14

    Java实现Zip压缩解压可以使用JDK的原生类java.util.zip,但是JDK 7 之前存在中文文件名乱码问题. 使用 ant.jar 的org.apache.tools.zip包,可以避免乱码问题. 使用专门的压缩解压第三方组件,如zip4j,zt-zip等,这种实现方式当然更强大,不过一般场景用不到,如果只是压缩解压第2种方式足够了. 本文使用的是第2种方式:代码猛击这里 实现:压缩(同一个API支持文件文件夹)&& 解压缩 实现过程中主要参考了:这篇博客 ,在此表示感谢. 关

  • 编译环境与生成环境的JDK版本不一样,报:java.util.zip.ZipException: 2014-09-10

    发布应用的时候,发现生产环境报将编译环境编译好的WAR包拿到生产环境,报异常如下: [java] view plain copy Caused by: java.util.zip.ZipException: error in opening zip file at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:127) at java.util.jar.JarFile

  • java 压缩zip文件 2015-01-13

    package com.demo; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.

  • Extract the zip package to solve org.apache.tools Chinese garbled version of the conflict ant 2010-03-30

    private void createDirectory (String directory, String subDirectory) ( String dir []; File fl = new File (directory); try ( if (subDirectory == "" & & fl.exists ()! = true) fl.mkdir (); else if (subDirectory! = "") ( dir = subD

  • tar zip package. compression and decompression methods to the specified directory 2010-03-25

    Tar packing, compression and decompression methods to the specified directory linux tar is commonly used in packaging, compression, compression tools, many of his arguments, fold in just listed common parameters of compression and decompression Param