Java中对文件的解压缩
2015-12-25 15:21
477 查看
需要用到的jar包: apache-ant-1.9.3.jar
下载地址:http://ant.apache.org/bindownload.cgi
压缩方法
[java] view
plaincopyprint?
/**
* @desc 将源文件/文件夹生成指定格式的压缩文件,格式zip
* @param resourePath
* 源文件/文件夹
* @param targetPath
* 目的压缩文件保存路径
* 压缩到的位置,如果为null或空字符串则默认解压缩到跟源文件夹或者文件所在的同一目录
* @return void
*
* @throws Exception
*/
public static void compressedFile(String resourcesPath,String targetPath) throws Exception{
File resourcesFile = new File(resourcesPath); //源文件
String directoryPath = "";
//如果为null或空字符串则默认解压缩到跟源文件夹或者文件所在的同一目录
if (StringUtil.isEmpty(targetPath))
directoryPath = resourcesPath.substring(0,resourcesPath.replaceAll("\\*", "/").lastIndexOf("/"));
else
directoryPath = targetPath;
File targetFile = new File(directoryPath); //目的
//如果目的路径不存在,则新建
if(!targetFile.exists())
{
targetFile.mkdirs();
}
String targetName = resourcesFile.getName()+".zip"; //目的压缩文件名
FileOutputStream outputStream = new FileOutputStream(directoryPath+File.separator+targetName);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));
createCompressedFile(out, resourcesFile, "");
out.close();
}
/**
* @desc 生成压缩文件。 如果是文件夹,则使用递归,进行文件遍历、压缩
* 如果是文件,直接压缩
* @param out
* 输出流
* @param file
* 目标文件
* @return void
* @throws Exception
*/
public static void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{
//如果当前的是文件夹,则进行进一步处理
if(file.isDirectory()){
//得到文件列表信息
File[] files = file.listFiles();
//将文件夹添加到下一级打包目录
out.putNextEntry(new ZipEntry(dir+"/"));
dir = dir.length() == 0 ? "" : dir +"/";
//循环将文件夹中的文件打包
for(int i = 0 ; i < files.length ; i++)
{
createCompressedFile(out, files[i], dir + files[i].getName()); //递归处理
}
}
else
{ //当前的是文件,打包处理
//文件输入流
InputStream is = new FileInputStream(file);
out.putNextEntry(new ZipEntry(dir));
//进行写操作
int len = 0;
byte[] buffer = new byte[1024];
while((len = is.read(buffer)) > 0)
{
out.write(buffer,0,len);
}
//关闭输入流
is.close();
}
}
解压缩方法
[java] view
plaincopyprint?
/**
* 解压缩zip包
*
* @param zipFilePath
* zip文件路径
* @param targetPath
* 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
* @throws IOException
*/
public static void decompressionFile(String zipFilePath, String targetPath)throws IOException {
ZipFile zipFile = new ZipFile(zipFilePath);
String directoryPath = "";
//如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
if (StringUtil.isEmpty(targetPath))
directoryPath = zipFilePath.substring(0,zipFilePath.lastIndexOf("."));
else
directoryPath = targetPath;
Enumeration<ZipEntry> entryEnum = zipFile.getEntries();
if (entryEnum != null)
{
ZipEntry zipEntry = null;
while (entryEnum.hasMoreElements())
{
zipEntry = (ZipEntry) entryEnum.nextElement();
//如果为目录
if (zipEntry.isDirectory())
{
System.out.println("文件夹路劲--------------"+directoryPath + File.separator+ zipEntry.getName());
File target = new File(directoryPath + File.separator+ zipEntry.getName());
target.mkdirs();
continue;
}
if (zipEntry.getSize() !=-1)
{
// 文件
File targetFile = new File(directoryPath+ File.separator + zipEntry.getName());
//如果目的文件夹的父目录为空,则创建
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
targetFile = new File(targetFile.getAbsolutePath());
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile));
InputStream is = zipFile.getInputStream(zipEntry);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer, 0, 1024)) >= 0)
{
os.write(buffer, 0, len);
}
os.close();
is.close();
}
}
}
}
这里提到一点为什么只能解压缩zip格式,其原因是windows和linux两者都兼容的只有zip文件。
下载地址:http://ant.apache.org/bindownload.cgi
压缩方法
[java] view
plaincopyprint?
/**
* @desc 将源文件/文件夹生成指定格式的压缩文件,格式zip
* @param resourePath
* 源文件/文件夹
* @param targetPath
* 目的压缩文件保存路径
* 压缩到的位置,如果为null或空字符串则默认解压缩到跟源文件夹或者文件所在的同一目录
* @return void
*
* @throws Exception
*/
public static void compressedFile(String resourcesPath,String targetPath) throws Exception{
File resourcesFile = new File(resourcesPath); //源文件
String directoryPath = "";
//如果为null或空字符串则默认解压缩到跟源文件夹或者文件所在的同一目录
if (StringUtil.isEmpty(targetPath))
directoryPath = resourcesPath.substring(0,resourcesPath.replaceAll("\\*", "/").lastIndexOf("/"));
else
directoryPath = targetPath;
File targetFile = new File(directoryPath); //目的
//如果目的路径不存在,则新建
if(!targetFile.exists())
{
targetFile.mkdirs();
}
String targetName = resourcesFile.getName()+".zip"; //目的压缩文件名
FileOutputStream outputStream = new FileOutputStream(directoryPath+File.separator+targetName);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));
createCompressedFile(out, resourcesFile, "");
out.close();
}
/**
* @desc 生成压缩文件。 如果是文件夹,则使用递归,进行文件遍历、压缩
* 如果是文件,直接压缩
* @param out
* 输出流
* @param file
* 目标文件
* @return void
* @throws Exception
*/
public static void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{
//如果当前的是文件夹,则进行进一步处理
if(file.isDirectory()){
//得到文件列表信息
File[] files = file.listFiles();
//将文件夹添加到下一级打包目录
out.putNextEntry(new ZipEntry(dir+"/"));
dir = dir.length() == 0 ? "" : dir +"/";
//循环将文件夹中的文件打包
for(int i = 0 ; i < files.length ; i++)
{
createCompressedFile(out, files[i], dir + files[i].getName()); //递归处理
}
}
else
{ //当前的是文件,打包处理
//文件输入流
InputStream is = new FileInputStream(file);
out.putNextEntry(new ZipEntry(dir));
//进行写操作
int len = 0;
byte[] buffer = new byte[1024];
while((len = is.read(buffer)) > 0)
{
out.write(buffer,0,len);
}
//关闭输入流
is.close();
}
}
解压缩方法
[java] view
plaincopyprint?
/**
* 解压缩zip包
*
* @param zipFilePath
* zip文件路径
* @param targetPath
* 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
* @throws IOException
*/
public static void decompressionFile(String zipFilePath, String targetPath)throws IOException {
ZipFile zipFile = new ZipFile(zipFilePath);
String directoryPath = "";
//如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
if (StringUtil.isEmpty(targetPath))
directoryPath = zipFilePath.substring(0,zipFilePath.lastIndexOf("."));
else
directoryPath = targetPath;
Enumeration<ZipEntry> entryEnum = zipFile.getEntries();
if (entryEnum != null)
{
ZipEntry zipEntry = null;
while (entryEnum.hasMoreElements())
{
zipEntry = (ZipEntry) entryEnum.nextElement();
//如果为目录
if (zipEntry.isDirectory())
{
System.out.println("文件夹路劲--------------"+directoryPath + File.separator+ zipEntry.getName());
File target = new File(directoryPath + File.separator+ zipEntry.getName());
target.mkdirs();
continue;
}
if (zipEntry.getSize() !=-1)
{
// 文件
File targetFile = new File(directoryPath+ File.separator + zipEntry.getName());
//如果目的文件夹的父目录为空,则创建
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
targetFile = new File(targetFile.getAbsolutePath());
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile));
InputStream is = zipFile.getInputStream(zipEntry);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer, 0, 1024)) >= 0)
{
os.write(buffer, 0, len);
}
os.close();
is.close();
}
}
}
}
这里提到一点为什么只能解压缩zip格式,其原因是windows和linux两者都兼容的只有zip文件。
相关文章推荐
- java基础之枚举用法
- 在多核操作系统中,java多线程不能被指定到具体cpu上,而是由操作系统自动分配
- eclipse svn subclipse下载地址
- 新手之服务端创建服务以及客户端的实现
- java中的Actor模式 Akka实例
- 64位Ubuntu,Eclipse新建Android project后缺失R文件的原因分析及解决办法
- Java猜拳小游戏
- spring 事务传播问题
- java文件下载的文件名乱码问题的解决
- 关于阿里云ACE中JAVA平台的一些注意事项
- java并发编程中CountDownLatch和CyclicBarrier的使用
- Quartz2.2 java实例所遇到的问题
- 给你的Java程序加上配置文件
- MyEclipse项目报错:Target runtime com.genuitec.runtime.generic.jee60 is not defined
- Eclipse 活动标签标题乱码问题
- MyBatis学习总结(8)——Mybatis3.x与Spring4.x整合
- Java Map排序
- Java中Properties类的操作
- java编辑dbf文件
- java7 switch case支持字符串的原理解析