第一步:文件压缩和解压缩方法

 //解压gzip文件public static boolean extractZip(File file, File parent) {ZipFile zf = null;try {zf = new ZipFile(file);Enumeration<? extends ZipEntry> entries = zf.entries();if (entries == null)return false;final byte[] buf = new byte[256];while (entries.hasMoreElements()) {ZipEntry entry = entries.nextElement();if (entry == null)continue;if (entry.isDirectory()) {File dir = new File(parent, entry.getName());dir.mkdirs();continue;}File dstFile = new File(parent, entry.getName());if (!dstFile.exists()) {dstFile.getParentFile().mkdirs();}InputStream fis = zf.getInputStream(entry);BufferedInputStream bis = new BufferedInputStream(fis);FileOutputStream fos = new FileOutputStream(dstFile);BufferedOutputStream bos = new BufferedOutputStream(fos);int read = 0;while ((read = bis.read(buf)) > 0) {bos.write(buf, 0, read);}fis.close();bis.close();bos.close();fos.close();}return true;} catch (Exception e) {e.printStackTrace();return false;} finally {try {zf.close();} catch (Exception e) {e.printStackTrace();}}}/*** 对将单个文件进行压缩* * @param source*            源文件* @param target*            目标文件* @throws IOException*/public static void zipFile(String source, String target) throws IOException {FileInputStream fin = null;FileOutputStream fout = null;GZIPOutputStream gzout = null;try {fin = new FileInputStream(source);fout = new FileOutputStream(target);gzout = new GZIPOutputStream(fout);byte[] buf = new byte[1024];int num;while ((num = fin.read(buf)) != -1) {gzout.write(buf, 0, num);}} finally {if (gzout != null)gzout.close();if (fout != null)fout.close();if (fin != null)fin.close();}}//多个文件压缩成gzip文件public static void mutileFileToGzip(ArrayList<String> filePaths, String targetFileName) {try {File file = new File(targetFileName);FileOutputStream fout = new FileOutputStream(file);BufferedInputStream bin = null;ZipOutputStream zout = new ZipOutputStream(fout);for (String fileSource : filePaths) {String[] fileNames = fileSource.split("/");zout.putNextEntry(new ZipEntry(fileNames[fileNames.length - 1]));int c;bin = new BufferedInputStream(new FileInputStream(fileSource));while ((c = bin.read()) != -1) {zout.write(c);}bin.close();}zout.close();} catch (Exception e) {e.printStackTrace();}System.out.println("压缩成功!");}

  第二步、如何调用?

1、将多个文件打包成gzip:

ArrayList<String> filePaths = new ArrayList<String>();
filePaths.add(memeDirectoryName + "/meta.json");
imgPath=java.net.URLDecoder.decode(imgPath,"UTF-8");
filePaths.add(imgPath);
String targetZipFileName = memeDirectoryName + File.separator + gzipName;
targetZipFileName=java.net.URLDecoder.decode(targetZipFileName,"UTF-8");
FileUtils.mutileFileToGzip(filePaths, targetZipFileName);

2、gzip解压成多个文件

FileUtils.extractZip(new File("/Users/figo/Downloads/memepackages/test1/test1.gzip"), new File("/Users/figo/Downloads/memepackages/test1/"));

3、如何获取当前文件所在的路径?

public static String getRealPath() {String realPath = "";try {File file = new File(ReadExcelWriteZip.class.getResource("/").getPath());// 显示该jar所在的路径// String realPath// =ReadExcelWriteZip.class.getClassLoader().getResource("").getFile();realPath = file.getAbsolutePath();realPath=java.net.URLDecoder.decode(realPath,"UTF-8"); } catch (Exception e) {if (e != null) {e.printStackTrace();realPath = e.getMessage();}}return realPath;}

4、如何通过命令行调用jar文件

通过eclipse导出可执行的jar,其实双击这个jar就可以执行里面的Main函数的,不过也可以写一个bat文件,双击后在windows操作系统运行

bat文件这么写:      java -jar  test.jar

将多个文件压缩成gzip,将gzip解压成多个文件相关推荐

  1. linux 解压rar格式的文件怎么打开,linux服务器怎么解压rar格式的文件

    做seo的很多人都会经历过这个问题,那就是安装的宝塔.wdcp等面板后,但是在上传完压缩文件的时候,发现压缩文件是rar后缀的压缩文件,在宝塔面板中就解压不了rar文件. 一.于是就想着把后缀改为zi ...

  2. python h5s文件 压缩_如何用python解压zip压缩文件

    前言:python在办公上非常有用,它可以解压文件,可以处理表格,还可以操作浏览器,只要你能想到的功能,它都能做到,今天笔者就为大家介绍一下如何用处理压缩文件. 打开cmd,输入python3,今天的 ...

  3. Linux 打包压缩-文件压缩-压缩工具【unzip】解压使用基本介绍

    unzip 用于解压缩由zip命令压缩的压缩包 补充说明 unzip命令 用于解压缩由zip命令压缩的".zip"压缩包. 语法 unzip(选项)(参数) 选项 -c:将解压缩的 ...

  4. 【压缩技巧】如何把文件压缩成RAR?如何解压?

    在生活及工作中,我们经常需要压缩文件使其更方便存储或传送. 压缩文件的格式有很多种,RAR是其中一种常见的格式,也是WinRAR软件独有的压缩格式.所以想要把文件压缩成RAR,就可以通过WinRAR来 ...

  5. C#压缩或解压(rar和zip文件)

    为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar.zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库. 在C#. ...

  6. ubuntu18.04 Linux包文件解压和安装,文件夹压缩打包

    目录 一.bz2 二.zip和unzip 三.tar .tar.gz tar.xz .tgz 四.deb 五.7z [无法输入中文]Ubuntu18.04中使用中文输入法_Linux教程_云网牛站 - ...

  7. 不需要解压使用对pdf文件进行压缩

    我们在压缩文件的时候通常是将文件压缩为zip这种格式,然后在我们需要使用上的时候再将文件进行解压,这样压缩文件的方法是2345.360压缩的原理,需要解压还得安装相应的解压软件才能实现. 小编最近因为 ...

  8. 压缩多张图片,解压后发现图片文件损坏的问题解决

    最近项目里需要做一个功能,就是吧多张图片压缩一个压缩文件,我使用的是java的ZipOutputStream 来实现的.最后开发完了,测试发现解压后的图片文件只有第一张能打开,后面的几张图片都打不开. ...

  9. 关于taz文件解压成tar文件这件事(根源在于7-Zip)

    最近在看图像分类的代码,从劈里啪啦那里得到一份花分类的数据集 具体的实现过程参照pytorch--AlexNet--训练花分类数据集_heart_6662的博客-CSDN博客_花分类数据集 链接实现过 ...

最新文章

  1. Python爬虫破解有道翻译
  2. frdora10_a8_linux,在Fedora 10中安装IRAF
  3. Xcode 新项目删除 storyboard 文件
  4. 使用内核定时器的second字符设备驱动及测试代码
  5. 初探swift语言的学习笔记六(ARC-自动引用计数,内存管理)
  6. Kafka C++客户端库librdkafka笔记
  7. SpatiaLite空间索引(一)
  8. 【Java数据结构与算法】第十四章 红黑树
  9. 霓虹促销电商设计,购买欲冲击视觉
  10. C#如何查看DLL文件中的成员
  11. 企业级PHP后台开发框架
  12. 555定时器应用之--多谐振荡器
  13. 【设计开发命名必备】英语单词缩写规则
  14. 刘严涛一枚天蝎座的在读大学生
  15. 品高云暴漫 | 云计算足球赛之黑马
  16. 时间戳是什么?JS中时间戳的展示
  17. 华丽丽的旋转彩带效果
  18. idea做一个日志自动生成的jar包,并用flume做生产者,采集日志数据,用kafka做消费者来消费日志数据
  19. 数字编码电位器c语言,数字电位器x9c103应用电路
  20. Prometheus 搭建监控系统

热门文章

  1. canvas 绘制圆弧
  2. 成都信息工程大学计算机学院复试指南
  3. SQL Server创建表语句介绍
  4. 矩阵的秩和图像的秩的一些了解
  5. 什么是交互设计?(附视频教程)
  6. PMON环境配置(龙芯,亲测成功1)
  7. Java实现连接数据库验证登录和注册(附详细知识点讲解)
  8. Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven...
  9. jdk新特性::作用
  10. 群联PS2251-67成功量产教程