JAVA自带API的压缩与解压
Java API中的 java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作。我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作。
ZipFile
-
File file = new File("F:/zippath.zip");
-
ZipFile zipFile = new ZipFile(file);
-
System.out.println("压缩文件的名称为:" + zipFile.getName());
压缩单个文件
-
/** 压缩单个文件*/
-
public static void ZipFile(String filepath ,String zippath) {
-
try {
-
File file = new File(filepath);
-
File zipFile = new File(zippath);
-
InputStream input = new FileInputStream(file);
-
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
-
zipOut.putNextEntry(new ZipEntry(file.getName()));
-
int temp = 0;
-
while((temp = input.read()) != -1){
-
zipOut.write(temp);
-
}
-
input.close();
-
zipOut.close();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
应用:
ZipFile("d:/hello.txt", "d:/hello.zip");
压缩多个文件(文件夹)
-
/** 一次性压缩多个文件,文件存放至一个文件夹中*/
-
public static void ZipMultiFile(String filepath ,String zippath) {
-
try {
-
File file = new File(filepath);// 要被压缩的文件夹
-
File zipFile = new File(zippath);
-
InputStream input = null;
-
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
-
if(file.isDirectory()){
-
File[] files = file.listFiles();
-
for(int i = 0; i < files.length; ++i){
-
input = new FileInputStream(files[i]);
-
zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
-
int temp = 0;
-
while((temp = input.read()) != -1){
-
zipOut.write(temp);
-
}
-
input.close();
-
}
-
}
-
zipOut.close();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
应用:
ZipMultiFile("f:/uu", "f:/zippath.zip");
解压缩单个文件
-
/** 解压缩(解压缩单个文件)*/
-
public static void ZipContraFile(String zippath ,String outfilepath ,String filename) {
-
try {
-
File file = new File(zippath);//压缩文件路径和文件名
-
File outFile = new File(outfilepath);//解压后路径和文件名
-
ZipFile zipFile = new ZipFile(file);
-
ZipEntry entry = zipFile.getEntry(filename);//所解压的文件名
-
InputStream input = zipFile.getInputStream(entry);
-
OutputStream output = new FileOutputStream(outFile);
-
int temp = 0;
-
while((temp = input.read()) != -1){
-
output.write(temp);
-
}
-
input.close();
-
output.close();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
应用:
ZipContraFile("d:/hello.zip","d:/eee.txt", "hello.txt");
解压缩多个文件
-
/** 解压缩(压缩文件中包含多个文件)可代替上面的方法使用。
-
* ZipInputStream类
-
* 当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,
-
* 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类
-
* */
-
public static void ZipContraMultiFile(String zippath ,String outzippath){
-
try {
-
File file = new File(zippath);
-
File outFile = null;
-
ZipFile zipFile = new ZipFile(file);
-
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
-
ZipEntry entry = null;
-
InputStream input = null;
-
OutputStream output = null;
-
while((entry = zipInput.getNextEntry()) != null){
-
System.out.println("解压缩" + entry.getName() + "文件");
-
outFile = new File(outzippath + File.separator + entry.getName());
-
if(!outFile.getParentFile().exists()){
-
outFile.getParentFile().mkdir();
-
}
-
if(!outFile.exists()){
-
outFile.createNewFile();
-
}
-
input = zipFile.getInputStream(entry);
-
output = new FileOutputStream(outFile);
-
int temp = 0;
-
while((temp = input.read()) != -1){
-
output.write(temp);
-
}
-
input.close();
-
output.close();
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
应用:
-
ZipContraMultiFile("f:/zippath.zip", "d:/");
-
ZipContraMultiFile("d:/hello.zip", "d:/");
JAVA自带API的压缩与解压的更多相关文章
- 自定义DelegatingHandler为ASP.NET Web Api添加压缩与解压的功能
HTTP协议中的压缩 Http协议中使用Accept-Encoding和Content-Encoding头来表示期望Response内容的编码和当前Request的内容编码.而Http内容的压缩其实是 ...
- JAVA实现实用的ZIP压缩与解压
http://blog.csdn.net/z69183787/article/details/38555913
- 文件压缩、解压工具类。文件压缩格式为zip
package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...
- Java实现文件压缩与解压
Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例.(转载自http://www.puiedu. ...
- Java实现文件压缩与解压[zip格式,gzip格式]
Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例. zip扮演着归档和压缩两个角色:gzip并 ...
- HttpClient与APS.NET Web API:请求内容的压缩与解压
首先说明一下,这里的压缩与解压不是通常所说的http compression——那是响应内容在服务端压缩.在客户端解压,而这里是请求内容在客户端压缩.在服务端解压. 对于响应内容的压缩,一般Web服务 ...
- java压缩文件解压:调用WinRAR5命令强于自己写代码实现
最近,手上维护着一个几年前的系统,技术是用的JSP+Strust2,系统提供了rar和zip两种压缩格式的解压功能,后台是用java实现的 1.解压rar格式,采用的是java-unrar-0.3.j ...
- java zip 压缩与解压
java zip 压缩与解压 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java. ...
- PAT(B) 1078 字符串压缩与解压(Java)
题目链接:1078 字符串压缩与解压 (20 point(s)) 题目描述 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示 ...
随机推荐
- regexp模式匹配+location页面跳转+cookie/localstorage本地存储
学习js的过程中,根据知识点编写一些code进行测试,以便检验. 这段程序使用了以下知识点: 1.regexp,对数据进行模式匹配 2.使用location对象进行页面跳转. 3.cookie/loc ...
- 关于python的二维数组
test =[ [1, 2, 3], [4, 5, 6], [7, 8, 9]] #这个就可以看做是二维数组了,直接创建print(test)print(test[:][1]) ...
- C++——多态性实现机制
C++的多态性实现机制剖析 1. 多态性和虚函数 #include <iostream.h> class animal { public: void sleep() { cout<& ...
- 【例题3-2 UVA - 10082】WERTYU
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用一个字符数组,用数组的下标相邻来代表相邻的关系. [错的次数] 在这里输入错的次数 [反思] int i; for (i = 0; ...
- 【BZOJ 4518】[Sdoi2016]征途
[链接] 链接 [题意] 在这里输入题意 [题解] DP+斜率优化; \(D(x) = E(x^2)-E(x)^2\) 其中\(E(x)^2\)这一部分是确定的. 因为总长是确定的,分成的段数又是确定 ...
- POJ 3132 & ZOJ 2822 Sum of Different Primes(dp)
题目链接: POJ:id=3132">http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/show ...
- ModSecurity防御暴力破解
http://www.modsecurity.org/ ModSecurity防御暴力破解 在阅读本文前,先简单了解下什么是ModSecurity,ModSecurity是一个入侵探测与阻止的引擎.它 ...
- github-vimium-compile-crx
chrome-62.x版本上安装vimium 1.61.1 https://blog.csdn.net/ZHUJIANWEILI4/article/details/78385346
- java之 ------ 枚举类型
枚举 一.枚举类型具体说明 简单的说.Enum一般用来表示一组同样类型的常量. 如性别.日期.月份.颜色等.对这些属性用常量的优点是显而易见的,不仅能够保证单例,且在比較的时候能够用"==& ...
- ios开发runtime学习三:动态添加方法(实际应用少,面试)
#import "ViewController.h" #import "Person.h" /* 1: Runtime(动态添加方法):OC都是懒加载机制,只要 ...