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)) 题目描述 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示 ...
随机推荐
- 基于bootstrap的主流框架有哪些
基于bootstrap的主流框架有哪些 一.总结 一句话总结:其实可以直接百度bootstrap后台模板,出来一大堆,想用哪个用哪个. 二.[前端框架系列]浅谈当前基于bootstrap框架的几种主流 ...
- 在云服务器上(CentOS)上安装Node
今天手抖,买了台云服务器,本人对服务器啥的基本不懂,linux命令基本靠度娘,所以连装个node环境都历经坎坷,搞了一下午终于搞好了,记录一下: 第一步:当然是先登录服务器了,打开命令行窗口,输入: ...
- (转)Windows2008优化IIS7.5支持10万个同时请求的配置方法
通过对IIS7的配置进行优化,调整IIS7应用池的队列长度,请求数限制,TCPIP连接数等方面,从而使WEB服务器的性能得以提升,保证WEB访问的访问流畅. 在运行中cmd后,输入:C:\Window ...
- [Angular] Learn How To Use ng-template Inputs
For example, we have a modal component, it can config that using ng-template as a configurable templ ...
- [Ramda] Pluck & Props: Get the prop(s) from object array
Pluck: Get one prop from the object array: R.pluck(}, {a: }]); //=> [1, 2] R.pluck()([[, ], [, ]] ...
- 对照jQuery和AngularJS的不同思维模
对照jQuery和AngularJS的不同思维模 Question 如果我已经熟悉了怎样使用jQuery来开发client应用.我如今打算使用AngularJS.请描写叙述一下有那些思维模式方面的东西 ...
- iOS8新特性
1. App Extension Programming Guide 2.LocalAuthentication.framework - Touch ID Authentication 3.Local ...
- u8和unsigned char的区别
- vue项目build后font-awesome不显示问题
解决办法: 修改build目录下的utils.js:添加 publicPath: '../../' // Extract CSS when that option is specified // (w ...
- Java链接Redis时出现 “ERR Client sent AUTH, but no password is set” 异常的原因及解决办法
Java链接Redis时出现 "ERR Client sent AUTH, but no password is set" 异常的原因及解决办法 [错误提示] redis.clie ...