admin 管理员组文章数量: 1184232
/**
* 解压缩zip格式的压缩文件.
*/
public static void unZip(File srcFile, String destDirPath)
throws RuntimeException
{
// 判断源文件是否存在
if (!srcFile.exists())
{
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
}
// 开始解压
ZipFile zipFile = null;
try
{
Charset gbk = Charset.forName("gbk");
zipFile = new ZipFile(srcFile,gbk);
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements())
{
ZipEntry entry = (ZipEntry)entries.nextElement();
System.out.println("解压" + entry.getName());
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory())
{
String dirPath = destDirPath + File.separator + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
}
else
{
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
File targetFile = new File(destDirPath + "/" + entry.getName());
// 保证这个文件的父文件夹必须要存在
if (!targetFile.getParentFile().exists())
{
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[BUFFER_SIZE];
while ((len = is.read(buf)) != -1)
{
fos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
fos.close();
is.close();
}
}
}
catch (Exception e)
{
throw new RuntimeException("unzip error from ZipUtils", e);
}
finally
{
if (zipFile != null)
{
try
{
zipFile.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
版权声明:本文标题:ZIP迷宫,一键通路,轻松穿越至文件原地 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1771437239a3544607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论