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)) 题目描述 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示 ...
随机推荐
- Android 快速下载 Android framework 源码
官网 Android framework源码git地址 github: https://github.com/android/platform_frameworks_base google 官方: h ...
- 代码高亮显示——google-code-prettify
先放着,搭建完HEXO博客再来写这篇. https://code.google.com/archive/p/google-code-prettify/
- Linux定时器的使用(三种方法)
使用定时器的目的无非是为了周期性的执行某一任务,或者是到了一个指定时间去执行某一个任务.要达到这一目的,一般有两个常见的比较有效的方法.一个是用linux内部的三个定时器,另一个是用sleep, us ...
- Java性能优化技巧集锦
一.通用篇 "通用篇"讨论的问题适合于大多数Java应用. 1.1 不用new关键词创建类的实例 用new关键词创建类的实例时,构造函数链中的全部构造函数都会被自己主动调用.但假设 ...
- MVC模式编程演示样例-登录验证(静态)
好,上篇博客分享了本人总结的JSP-Servlet-JavaBean三层架构编程模式的实现思想和基本流程,接下来给大家分享一个MVC编程模式的实现演示样例-登录验证的过程,这里我仍然用的是静态的验证u ...
- linux中的rootfs/initrd/ramfs/initramfs
什么是ramfs?ramfs是空间规模动态变化的RAM文件系统.它非常简单,是用来实现Linux缓存机制(缓存page cache and dentry cache)的文件系统.通常情况下,Linux ...
- Iaas、Paas和Saas的区别
Iaas: Infrastructure-as-a-service(基础设施即服务),Iaas上购买的一般是主机,用户不光要开发程序,还要考虑搭建系统,维护运行环境,以及怎么容灾,怎么做到高可用,怎么 ...
- 21、IIS声卡驱动程序
声卡芯片的数据通道一般都是IIS接口,但是控制音量等控制信息的接口都不相同 (新内核在linux-3.4.2\sound\soc\codecs\uda134x.c) uda134x_codec_pro ...
- [AngularFire2] Auth with Firebase auth -- email
First, you need to enable the email auth in Firebase console. Then implement the auth service: login ...
- Linux系统编程——线程私有数据
在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...