package com.green.project.compress;

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 {

/**
* @param args
*/
byte doc[]=null;
String Filename=null;
String unzipPath=null;

public Unzip(String filename,String unzippath)
{
Filename=filename;
unzipPath=unzippath;
SetUnZipPath(unzipPath);
}

public Unzip(String filename)
{
Filename=filename;
unzipPath=null;
SetUnZipPath(unzipPath);
}

private void SetUnZipPath(String unzippath)
{
if(unzippath.endsWith("//")) //String类的endsWith(char c)用与判断字符串最后一个字符是否与c相同
unzipPath=new String(unzippath);
else
unzipPath=new String(unzippath+"//");
}

public void doUnZip()
{
try {
ZipInputStream zin=new ZipInputStream(new FileInputStream(Filename));
ZipEntry fentry; //用于表示 ZIP 文件条目
while((fentry=zin.getNextEntry())!=null) //ZipInputStream类的geNextEntry()读取下一个 ZIP 文件条目并将流定位到该条目数据的开始处。
{
if(fentry.isDirectory()) //判断文件条目是否目录条目
checkFilePath(unzipPath+fentry.getName());
else
{
String fname=new String(unzipPath+fentry.getName());
FileOutputStream out=new FileOutputStream(fname);
doc=new byte[512];
int n;
while((n=zin.read(doc, 0, 512))!=-1)out.write(doc,0,n);
out.close();
out=null;
doc=null;
}
}//while
zin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private void checkFilePath(String dirName)throws IOException
{
File dir=new File(dirName);
if(!dir.exists())
dir.mkdirs();
}

public static void main(String[] args) {
Unzip myZip=new Unzip("d://phonemanage.zip","d://lib");
myZip.doUnZip();
System.out.println("压缩成功");
}

}

最新文章

  1. linux 内核学习之五 system_call过程分析
  2. JS rem 设置
  3. UVA 12050 - Palindrome Numbers 模拟
  4. IE10、IE11 无法写入Cookie
  5. jQuery基础选择器
  6. java集合总结
  7. 【数学三角恒等变幻】【HDU2552】三足鼎立
  8. 使用Lock实现信号量
  9. HDU 2112 HDU Today (Dijkstra算法)
  10. js中的true,false盲点
  11. JAVA包名、类名、变量名命名规则
  12. Codeblocks快捷键
  13. Servlet不再是烦恼
  14. 使用DOS命令无法启动MySQL
  15. 替换元素(replace,replace_if,replace_copy,replace_copy_if)
  16. vs2017配置pthread.h的方法
  17. vc MFC 通过IDispatch调用默认成员函数
  18. Robolectric使用教程
  19. ASP.NET MVC 编程参考
  20. POJ 3279 Fliptile 状态压缩,思路 难度:2

热门文章

  1. Quartz.Net CronExpression表达式详解
  2. python中时间和时区
  3. 旅行计划-DAG上最长路
  4. 学习练习 java面向对象存取款查询余额
  5. DNS协议 实践
  6. MyEclipse简单设置
  7. noip2010提高组题解
  8. 如何初始化一个iOS原生地图
  9. Mac OS X Server 安装与应用
  10. 用Zend Studio12 导入在workspace中的项目