JAVA 压缩文件和解压文件
来源:互联网 发布:pc护眼软件 编辑:程序博客网 时间:2024/05/22 17:45
JAVA 压缩文件和解压文件
使用到Apache的一个开源Jar包ant-1.9.4.jar
Java 压缩文件
public void zip(List<File> files, String zipFilepath) throws BuildException, RuntimeExceptioan { Zip zip = new Zip(); for (File file : files) { Project proj = new Project(); FileSet fileSet = new FileSet(); fileSet.setProject(proj); fileSet.setFile(file); //这里是将给定的file压缩起来,也可以将给定的dir压缩起来,需要使用fileSet.setDir() zip.setProject(proj); zip.addFileset(fileSet); } zip.setDestFile(new File(zipFilepath)); zip.setEncoding("utf-8"); zip.execute(); }
压缩文件就是这样,可以将给定的文件压缩起来,也可以将给定的dir压缩起来。
Java解压文件
public static void unZipFiles(File zipFile){ String unZipPath = zipFile.getParentFile().getPath() + "/" + zipFile.getName().substring(0, zipFile.getName().lastIndexOf(".")); try { ZipFile zf = new ZipFile(zipFile); ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile)); ZipEntry zipEntry = null; while((zipEntry = zipInputStream.getNextEntry()) != null){ String fileName = zipEntry.getName(); File temp = new File(unZipPath + "/" + fileName); System.out.println(temp.getPath()); if (!temp.getParentFile().exists()) { temp.getParentFile().mkdirs(); } OutputStream os = new FileOutputStream(temp); InputStream is = zf.getInputStream(zipEntry); int len = 0; while((len = is.read()) != -1){ os.write(len); } os.close(); is.close(); } zipInputStream.close(); } catch (ZipException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
这个就是解压文件。
仅供自己参考
0 0
- java压缩文件和解压文件
- JAVA 压缩文件和解压文件
- java压缩文件和解压文件
- java解压和压缩文件
- Java 解压和压缩文件(文件夹)
- JAVA压缩文件、解压文件、删除文件
- unix解压文件和压缩文件操作
- 用GZIP压缩文件和解压文件
- java zip和rar压缩文件的解压
- C#压缩文件,解压文件。
- Ubuntu压缩文件解压文件
- linux压缩文件&解压文件
- java 解压zip压缩文件
- java解压RAR压缩文件
- Java解压、压缩文件
- java解压RAR压缩文件
- java 解压压缩文件
- java解压缩文件和不解压读取压缩文件中的文本文件内容
- 黑马程序员-Java基础:面向对象
- LayoutInflater 与 inflate
- MongoDB 3.X Crud 增删查改
- 再说 c++11 内存模型
- hdu 1281 棋盘游戏
- JAVA 压缩文件和解压文件
- NPOI进行Excel文件的导出
- 传播最广的一篇SVM算法博文
- HDU 1321 Reverse Text
- 数据结构学习笔记4-最长回文子串(Manacher算法)
- Epoll学习心得
- hud-1863-畅通工程(最小生成树)
- svn属性svn:keywords与auto-props的配置(运行配置区)
- Codeforces 567D One-Dimensional Battle Ships