java实现文件的解压_Java实现文件解压缩源码

package com.zyuc.zipfile;

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.ZipException;

import java.util.zip.ZipFile;

import java.util.zip.ZipInputStream;

/**

* 解压ZIP压缩文件到指定的目录

* @title: UnZipFile

* @description:

* @author wangxw

* @since 2012-4-11下午01:55:22

* @version 1.0

*/

public final class UnZipFile {

/**

*

* 缓存区大小默认20480

*

*/

private final static int FILE_BUFFER_SIZE = 20480;

private UnZipFile() {}

/**

*

* 将指定目录的ZIP压缩文件解压到指定的目录

* @param zipFilePath ZIP压缩文件的路径

* @param zipFileName ZIP压缩文件名字

* @param targetFileDir ZIP压缩文件要解压到的目录

* @return flag 布尔返回值

*

*/

public static boolean unzip(String zipFilePath, String zipFileName, String targetFileDir){

boolean flag = false;

// 1.判断压缩文件是否存在,以及里面的内容是否为空

File file = null; // 压缩文件(带路径)

ZipFile zipFile = null;

file = new File(zipFilePath + "/" + zipFileName);

if(false == file.exists()) {

System.out.println(">>>>>>压缩文件【" + zipFilePath + "/" + zipFileName + "】不存在<<<<<

return false;

}else {

// 2.开始解压ZIP压缩文件的处理

System.out.println(">>>>>>解压文件【" + zipFilePath + "/" + zipFileName + "】到【" + targetFileDir + "】目录下<<<<<

byte[] buf = new byte[FILE_BUFFER_SIZE];

int readSize = -1;

ZipInputStream zis = null;

FileOutputStream fos = null;

try {

// 检查是否是zip文件

zipFile = new ZipFile(file);

zipFile.close();

// 判断目标目录是否存在,不存在则创建

File newdir = new File(targetFileDir);

if (false == newdir.exists()) {

newdir.mkdirs();

newdir = null;

}

zis = new ZipInputStream(new FileInputStream(file));

ZipEntry zipEntry = zis.getNextEntry();

// 开始对压缩包内文件进行处理

while (null != zipEntry) {

String zipEntryName = zipEntry.getName().replace('\\', '/');

// 判断zipEntry是否为目录,如果是,则创建

if(zipEntry.isDirectory()) {

int indexNumber = zipEntryName.lastIndexOf('/');

File entryDirs = new File(targetFileDir + "/" + zipEntryName.substring(0, indexNumber));

entryDirs.mkdirs();

entryDirs = null;

} else {

try {

fos = new FileOutputStream(targetFileDir + "/" + zipEntryName);

while ((readSize = zis.read(buf, 0, FILE_BUFFER_SIZE)) != -1) {

fos.write(buf, 0, readSize);

}

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e.getCause());

} finally {

try {

if (null != fos) {

fos.close();

}

} catch (IOException e) {

e.printStackTrace();

throw new RuntimeException(e.getCause());

}

}

}

zipEntry = zis.getNextEntry();

}

flag = true;

} catch (ZipException e) {

e.printStackTrace();

throw new RuntimeException(e.getCause());

} catch (IOException e) {

e.printStackTrace();

throw new RuntimeException(e.getCause());

} finally {

try {

if (null != zis) {

zis.close();

}

if (null != fos) {

fos.close();

}

} catch (IOException e) {

e.printStackTrace();

throw new RuntimeException(e.getCause());

}

}

}

return flag;

}

/**

*

* 测试用的Main方法

*

*/

public static void main(String[] args) {

String zipFilePath = "D:\\test";

String zipFileName = "qqqq.zip";

String targetFileDir = "D:\\test";

boolean flag = UnZipFile.unzip(zipFilePath, zipFileName, targetFileDir);

if(flag) {

System.out.println(">>>>>>解压成功<<<<<

} else {

System.out.println(">>>>>>解压失败<<<<<

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值