Java API中的 java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作。我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作。

ZipFile

java中的每一个压缩文件都是可以使用ZipFile来进行表示的。

  1. File file = new File("F:/zippath.zip");
  2. ZipFile zipFile = new ZipFile(file);
  3. System.out.println("压缩文件的名称为:" + zipFile.getName());

压缩单个文件


  1. /** 压缩单个文件*/
  2. public static void ZipFile(String filepath ,String zippath) {
  3. try {
  4. File file = new File(filepath);
  5. File zipFile = new File(zippath);
  6. InputStream input = new FileInputStream(file);
  7. ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
  8. zipOut.putNextEntry(new ZipEntry(file.getName()));
  9. int temp = 0;
  10. while((temp = input.read()) != -1){
  11. zipOut.write(temp);
  12. }
  13. input.close();
  14. zipOut.close();
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }

应用:

ZipFile("d:/hello.txt", "d:/hello.zip");

压缩多个文件(文件夹)


  1. /** 一次性压缩多个文件,文件存放至一个文件夹中*/
  2. public static void ZipMultiFile(String filepath ,String zippath) {
  3. try {
  4. File file = new File(filepath);// 要被压缩的文件夹
  5. File zipFile = new File(zippath);
  6. InputStream input = null;
  7. ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
  8. if(file.isDirectory()){
  9. File[] files = file.listFiles();
  10. for(int i = 0; i < files.length; ++i){
  11. input = new FileInputStream(files[i]);
  12. zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
  13. int temp = 0;
  14. while((temp = input.read()) != -1){
  15. zipOut.write(temp);
  16. }
  17. input.close();
  18. }
  19. }
  20. zipOut.close();
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }

应用:

ZipMultiFile("f:/uu", "f:/zippath.zip");

解压缩单个文件


  1. /** 解压缩(解压缩单个文件)*/
  2. public static void ZipContraFile(String zippath ,String outfilepath ,String filename) {
  3. try {
  4. File file = new File(zippath);//压缩文件路径和文件名
  5. File outFile = new File(outfilepath);//解压后路径和文件名
  6. ZipFile zipFile = new ZipFile(file);
  7. ZipEntry entry = zipFile.getEntry(filename);//所解压的文件名
  8. InputStream input = zipFile.getInputStream(entry);
  9. OutputStream output = new FileOutputStream(outFile);
  10. int temp = 0;
  11. while((temp = input.read()) != -1){
  12. output.write(temp);
  13. }
  14. input.close();
  15. output.close();
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }

应用:

ZipContraFile("d:/hello.zip","d:/eee.txt", "hello.txt");

解压缩多个文件

ZipInputStream类:
当我们需要解压缩多个文件的时候,ZipEntry就无法使用了。
 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类。

  1. /** 解压缩(压缩文件中包含多个文件)可代替上面的方法使用。
  2. * ZipInputStream类
  3. * 当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,
  4. * 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类
  5. * */
  6. public static void ZipContraMultiFile(String zippath ,String outzippath){
  7. try {
  8. File file = new File(zippath);
  9. File outFile = null;
  10. ZipFile zipFile = new ZipFile(file);
  11. ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
  12. ZipEntry entry = null;
  13. InputStream input = null;
  14. OutputStream output = null;
  15. while((entry = zipInput.getNextEntry()) != null){
  16. System.out.println("解压缩" + entry.getName() + "文件");
  17. outFile = new File(outzippath + File.separator + entry.getName());
  18. if(!outFile.getParentFile().exists()){
  19. outFile.getParentFile().mkdir();
  20. }
  21. if(!outFile.exists()){
  22. outFile.createNewFile();
  23. }
  24. input = zipFile.getInputStream(entry);
  25. output = new FileOutputStream(outFile);
  26. int temp = 0;
  27. while((temp = input.read()) != -1){
  28. output.write(temp);
  29. }
  30. input.close();
  31. output.close();
  32. }
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }

应用:


  1. ZipContraMultiFile("f:/zippath.zip", "d:/");
  2. ZipContraMultiFile("d:/hello.zip", "d:/");

JAVA自带API的压缩与解压的更多相关文章

  1. 自定义DelegatingHandler为ASP.NET Web Api添加压缩与解压的功能

    HTTP协议中的压缩 Http协议中使用Accept-Encoding和Content-Encoding头来表示期望Response内容的编码和当前Request的内容编码.而Http内容的压缩其实是 ...

  2. JAVA实现实用的ZIP压缩与解压

    http://blog.csdn.net/z69183787/article/details/38555913

  3. 文件压缩、解压工具类。文件压缩格式为zip

    package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...

  4. Java实现文件压缩与解压

    Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例.(转载自http://www.puiedu. ...

  5. Java实现文件压缩与解压[zip格式,gzip格式]

    Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例. zip扮演着归档和压缩两个角色:gzip并 ...

  6. HttpClient与APS.NET Web API:请求内容的压缩与解压

    首先说明一下,这里的压缩与解压不是通常所说的http compression——那是响应内容在服务端压缩.在客户端解压,而这里是请求内容在客户端压缩.在服务端解压. 对于响应内容的压缩,一般Web服务 ...

  7. java压缩文件解压:调用WinRAR5命令强于自己写代码实现

    最近,手上维护着一个几年前的系统,技术是用的JSP+Strust2,系统提供了rar和zip两种压缩格式的解压功能,后台是用java实现的 1.解压rar格式,采用的是java-unrar-0.3.j ...

  8. java zip 压缩与解压

    java zip 压缩与解压 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java. ...

  9. PAT(B) 1078 字符串压缩与解压(Java)

    题目链接:1078 字符串压缩与解压 (20 point(s)) 题目描述 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示 ...

随机推荐

  1. Android 快速下载 Android framework 源码

    官网 Android framework源码git地址 github: https://github.com/android/platform_frameworks_base google 官方: h ...

  2. 代码高亮显示——google-code-prettify

    先放着,搭建完HEXO博客再来写这篇. https://code.google.com/archive/p/google-code-prettify/

  3. Linux定时器的使用(三种方法)

    使用定时器的目的无非是为了周期性的执行某一任务,或者是到了一个指定时间去执行某一个任务.要达到这一目的,一般有两个常见的比较有效的方法.一个是用linux内部的三个定时器,另一个是用sleep, us ...

  4. Java性能优化技巧集锦

    一.通用篇 "通用篇"讨论的问题适合于大多数Java应用. 1.1 不用new关键词创建类的实例 用new关键词创建类的实例时,构造函数链中的全部构造函数都会被自己主动调用.但假设 ...

  5. MVC模式编程演示样例-登录验证(静态)

    好,上篇博客分享了本人总结的JSP-Servlet-JavaBean三层架构编程模式的实现思想和基本流程,接下来给大家分享一个MVC编程模式的实现演示样例-登录验证的过程,这里我仍然用的是静态的验证u ...

  6. linux中的rootfs/initrd/ramfs/initramfs

    什么是ramfs?ramfs是空间规模动态变化的RAM文件系统.它非常简单,是用来实现Linux缓存机制(缓存page cache and dentry cache)的文件系统.通常情况下,Linux ...

  7. Iaas、Paas和Saas的区别

    Iaas: Infrastructure-as-a-service(基础设施即服务),Iaas上购买的一般是主机,用户不光要开发程序,还要考虑搭建系统,维护运行环境,以及怎么容灾,怎么做到高可用,怎么 ...

  8. 21、IIS声卡驱动程序

    声卡芯片的数据通道一般都是IIS接口,但是控制音量等控制信息的接口都不相同 (新内核在linux-3.4.2\sound\soc\codecs\uda134x.c) uda134x_codec_pro ...

  9. [AngularFire2] Auth with Firebase auth -- email

    First, you need to enable the email auth in Firebase console. Then implement the auth service: login ...

  10. Linux系统编程——线程私有数据

    在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...