Java解压缩zip包

阅读更多
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;
        // 当目标目录为空的时候将文件解压到文件所在目录。
        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;

            // 创建解压后的文件夹。
            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();

            // 解压缩文件。
            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();
            }
        }
    }
}


直接来段源码。

你可能感兴趣的:(Java)