java.util.zip_[Java 基础] 使用java.util.zip包压缩和解压缩文件

Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作。

我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作。

ZipFile

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

File file = new File("F:/zippath.zip");

ZipFile zipFile= newZipFile(file);

System.out.println("压缩文件的名称为:" + zipFile.getName());

压缩单个文件

/**压缩单个文件*/

public static voidZipFile(String filepath ,String zippath) {try{

File file= newFile(filepath);

File zipFile= newFile(zippath);

InputStream input= newFileInputStream(file);

ZipOutputStream zipOut= new ZipOutputStream(newFileOutputStream(zipFile));

zipOut.putNextEntry(newZipEntry(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 voidZipMultiFile(String filepath ,String zippath) {try{

File file= new File(filepath);//要被压缩的文件夹

File zipFile = newFile(zippath);

InputStream input= null;

ZipOutputStream zipOut= new ZipOutputStream(newFileOutputStream(zipFile));if(file.isDirectory()){

File[] files=file.listFiles();for(int i = 0; i < files.length; ++i){

input= newFileInputStream(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 voidZipContraFile(String zippath ,String outfilepath ,String filename) {try{

File file= new File(zippath);//压缩文件路径和文件名

File outFile = new File(outfilepath);//解压后路径和文件名

ZipFile zipFile = newZipFile(file);

ZipEntry entry= zipFile.getEntry(filename);//所解压的文件名

InputStream input =zipFile.getInputStream(entry);

OutputStream output= newFileOutputStream(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类。

/**解压缩(压缩文件中包含多个文件)可代替上面的方法使用。

* ZipInputStream类

* 当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,

* 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类

**/

public static voidZipContraMultiFile(String zippath ,String outzippath){try{

File file= newFile(zippath);

File outFile= null;

ZipFile zipFile= newZipFile(file);

ZipInputStream zipInput= new ZipInputStream(newFileInputStream(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= newFileOutputStream(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:/");

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543235.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用好href的target, base href

<base href>就是指网页里面的相对链接 的前缀url&#xff0c;如在<head></head>部分定义了此链接为http://ent.sina.com.cn/ &#xff0c;那么下面的<a hrefaaa.html></a>代表http://ent.sina.com.cn/aaa.html 这个标签的用处是解决编程时候的相…

python遍历二维数组_在Python中遍历二维数组?

你需要告诉我们一些事情&#xff1a;dataset datas.values 它可能是一个二维数组&#xff0c;因为它来自一个csv负载。但是什么形状和数据类型&#xff1f;甚至可能是阵列的一个样本。 这是函数中的data参数吗&#xff1f; 什么是blackKings和values&#xff1f;你把它们当作列…

elementui 弹窗 显示详细信息_ElementUI中el-table双击单元格事件并获取指定列的值和弹窗显示详细信息...

场景双击el-table的某个单元格时获取此单元格的信息并弹窗显示其他关联的信息。效果如下注&#xff1a;实现首先给el-table设置cell-dblclick事件v-loading"loading":data"kqryszList"selection-change"handleSelectionChange"cell-dblclick&quo…

ESXI开启SNMP服务

要监控ESXI&#xff0c;打算通过snmp方式进行监控&#xff0c;这样操作比较简单。但是要使用SNMP方式进行监控&#xff0c;必须要开启ESXI的SNMP服务。ESXI由于版本号的不同&#xff0c;SNMP开启的方法也不尽相同。 我们先来介绍ESXI5.0开启方法。在开启之前&#xff0c;我们先…

fiddler修改支付金额_不容忽视的记账工具:支付宝记账

上一篇文章我们说到&#xff0c;微信记账是一个省心省力省时间的好工具&#xff0c;但平时我们的支付手段可能不只有微信&#xff0c;最常用的还有支付宝&#xff0c;例如我们在双十一淘宝上购物的话基本上使用的是支付宝进行付款&#xff0c;因此为大家普及一下支付宝的记账流…

php5.3 延迟静态绑定 static关键字

1 //传统模式 --这段代码能很好工作&#xff0c;但大量的重复代码很烦人,不想为每个DomainObject子类都创建这段相同代码吧&#xff1f;2 /*3 4 abstract class DomainObject{}5 6 class User extends DomainObject7 {8 public function __construct()9 { 10 e…

java 两点间距离_Java实现控制台输出两点间距离

计算两点之间的距离然后在控制台输出&#xff0c;这个题目还是挺简单的。下面我们来看看具体代码。package com.swift;import java.util.Scanner;public class PToP {public static void main(String[] args) {Scanner scannew Scanner(System.in);System.out.println("请…

微信小程序python解析获取用户手机号_python获取微信小程序openid及用户信息

填坑记录&#xff1a;1.个人微信公众订阅号是不能申请微信认证的。公众号的类型在注册时一旦选择就不能更改&#xff0c;微信公众号认证的功能除个人订阅号外&#xff0c;都可以申请认证&#xff0c;因而个人订阅号不能申请认证。这句话的意思就是个人订阅号无法开发小程序。 2…

使用 React和webpack开发和打包发布

建议在 React 中使用 CommonJS 模块系统&#xff0c;比如 browserify 或 webpack&#xff0c;本次使用 webpack。 第一步、安装全局包 $ npm install babel -g $ npm install webpack -g $ npm install webpack-dev-server -g 第二步、创建根目录 创建一个根目录&#xff0c;目…

java cpu 内存使用情况_java高cpu占用和高内存占用问题排查 (转)

高cpu占用1、top命令&#xff1a;Linux命令。可以查看实时的CPU使用情况。也可以查看最近一段时间的CPU使用情况。2、PS命令&#xff1a;Linux命令。强大的进程状态监控命令。可以查看进程以及进程中线程的当前CPU使用情况。属于当前状态的采样数据。ps -mp pid -o THREAD,tid,…

python3 装饰器_python3_装饰器_异常处理

装饰器&#xff1a; def auth(func): def wrapper(name): 如果函数带参数&#xff0c;要加在这里 userraw_input("input passwd:").strip() if user‘test‘: print "welcome login" func(name) 如果函数带参数&#xff0c;要加在这里 else: print "a…

ZeroMQ--使用jzmq进行编程

一、环境搭建 wget http://download.zeromq.org/zeromq-2.1.7.tar.gz tar -xzf zeromq-2.1.7.tar.gz cd zeromq-2.1.7 ./configure make sudo make installgit clone https://github.com/nathanmarz/jzmq.git cd jzmq ./autogen.sh ./configure make sudo make install如果没有…

java 循环对比_Java stream 和 for 循环效率对比问题

针对同一个集合&#xff0c;用 stream 操作两次得到两个不同条件筛选出来的集合和map&#xff0c;和一次for循环就搞定搞定的效率对比。虽然stream写起来链式操作很舒服&#xff0c;但效率在不同数据量下的体现效果是不一样的&#xff0c;以下为我的测试代码&#xff1a;Testpu…

python多线程框架_基于python和bash的多线程任务框架 不要让cpu闲着了

[Bash shell] 纯文本查看 复制代码#! /bin/bash ########################################################################################################## #part1:defile your job&#xff08;第一部分&#xff0c;定义单核任务&#xff0c;需要根据你的实用任务改动&a…

FreeSWITCH第三方库(视频)的简单介绍(二)

FreeSWITCH使用了大量的第三方库&#xff0c;本文档主要介绍视频相关库的信息&#xff1a; 音频相关库的信息介绍参考&#xff1a;http://www.cnblogs.com/yoyotl/p/5486753.html 其他相关库的信息介绍参考&#xff1a;http://www.cnblogs.com/yoyotl/p/5489315.html ① openh2…

python单步调试的方法_python断点调试方法

pdb 是 python 自带的一个包&#xff0c;为 python 程序提供了一种交互的源代码调试功能&#xff0c;主要特性包括设置断点、单步调试、进入函数调试、查看当前代码、查看栈片段、动态改变变量的值等。pdb 提供了一些常用的调试命令&#xff0c;详情见表 1。下面结合具体的实例…

053(七十七)

381、 381.Which pseudo column could you use to identify a unique row in a Flashback Versions Query? A. XID B. VERSIONS_PK C. VERSIONS_XID D. VERSIONS_UNIQUE 382、 382.Which of the following can be used in conjunction with a Flashback Versions Query to fil…

java正则表达式 ppt_Java正则表达式演示

//正则表达式 regular expression regex//空白符有&#xff1a; ,\t,\n,\n,\r,\h;\s等于[\t\n\r\f];\S[^\t\n\r\f]// 常用的正则表达式//// 正则表达式 匹配//-------------------------------…

数加移动定向营销:如何快速搭建不完全依赖用户历史行为的个性化服务

移动定向营销是一款基于规则引擎面向移动应用做流量精准运营的数据服务&#xff0c;能帮助使用者灵活划分用户群体、细分用户流量。移动定向营销原理它是一种定向的、人为可控的、可解释的&#xff0c;并且不完全依赖于用户历史行为、不受投放内容约束、面向用户的个性化服务方…

python读写json_python 读写json数据

json 模块提供了一种很简单的方式来编码和解码JSON 数据。 字符串操作 其中两个主要的函数是json.dumps() 和json.loads() &#xff0c;要比其他序列化函数库如pickle 的接口少得多。下面演示 1.如何将一个Python 数据结构转换为JSON 2.如何将一个JSON 编码的字符串转换回一个P…