Apache实现文件解压缩
来源:互联网 发布:java nio与io的区别 编辑:程序博客网 时间:2024/06/03 09:02
下面介绍的是使用Apache的Ant.jar 包来实现对文件的压缩和解压
首先是压缩文件的代码
fList 中的内容是文件的路径
ZipEntry 中用到的index是为了方便从文件路劲中截取文件名
//压缩文件使用使用Apache的ANT包下的方法public void fileToZip(List<String> fList,String targerFile,int index){//创建Zip输出流对象try {FileOutputStream fos=new FileOutputStream(targerFile);ZipOutputStream zipOut=new ZipOutputStream(fos);byte buffer[]=new byte[1024];for(String path:fList){//创建输入流对象FileInputStream fis=new FileInputStream(path);ZipEntry en=new ZipEntry(path.substring(index));zipOut.putNextEntry(en);int len=0;while((fis.read(buffer))!=-1){zipOut.write(buffer, 0, len);}zipOut.closeEntry();fis.close();}zipOut.close();fos.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
下面是对文件解压的代码
list主要用来记录一下都解压出了什么文件
//解压文件 使用Apache下的ANT包@SuppressWarnings("unchecked")public void ZIPtoFile(String sourceFile,String targerPath,List<String> list){//ZipFile zipFile=new ZipFile(sourceFile);//Enumeration<ZipEntry> e=zipFile.get ZipFile zf=null;try {zf = new ZipFile(sourceFile);Enumeration<ZipEntry> e = zf.getEntries();// 获得所有ZipEntry对象while(e.hasMoreElements()){ZipEntry zn=e.nextElement();if(!zn.isDirectory()){File newFile=new File(targerPath+File.separator+zn.getName());list.add(newFile.getAbsolutePath());new File(newFile.getParent()).mkdirs();//这里要注意,创建这个父路径;//newFile.mkdirs();newFile.createNewFile();FileOutputStream output=new FileOutputStream(newFile);InputStream in=zf.getInputStream(zn);int read=0;while((read=in.read())!=-1){output.write(read);}output.close();}}} catch (ZipException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} }使用Apache的Ant的好处是可以避免中文乱码的问题
阅读全文
0 0
- Apache实现文件解压缩
- 使用 apache ant 轻松实现文件压缩/解压缩
- 使用 apache ant 轻松实现文件压缩/解压缩 .
- java代理使用 apache ant实现文件压缩/解压缩
- c#解压缩文件实现
- ZipEntry实现文件解压缩与目录解压缩
- Java实现文件解压缩源码
- java实现压缩、解压缩文件
- 利用RAR实现文件压缩解压缩
- 用Java程序实现解压缩文件
- 实现在J2ME中解压缩ZIP文件
- JAVA实现压缩/解压缩zip文件
- 用Java实现文件的解压缩
- JAVA实现压缩,解压缩zip文件
- java 实现解压缩文件(ZIP/rar)
- 哈夫曼树实现文件压缩与解压缩
- linux中rar文件解压缩的实现
- 哈夫曼树实现文件的压缩与解压缩
- (CF 148D Bag of mice)<简单概率DP>
- android Annotion
- 简单的倒计时按钮
- 分布式机器学习平台大比拼
- Java实现一个简单的Web服务器
- Apache实现文件解压缩
- Android 自定义进度条ColorfulProgressBar,原理简单、效果很棒
- flask+mongodb实现简单todolist应用
- 实现读取文件夹中文件,复制到另外一个文件夹,并且删除原来的文件包括(图片和PDF)
- 初始化字典的6种方式
- C语言入门:冒泡排序(代码实现,而不是排序方法阐述)
- Android 常用sql笔记之视图创建
- HDU5340 Three Palindromes <Manacher>
- Guarding the Chessboard UVA