java 上文件传示例

Welcome to Java Unzip File Example. In the last post, we learned how to zip file and directory in java, here we will unzip the same zip file created from directory to another output directory.

欢迎使用Java解压缩文件示例。 在上一篇文章中,我们学习了如何在java中压缩文件和目录 ,这里我们将从目录创建的相同zip文件解压缩到另一个输出目录。

Java解压缩文件 (Java Unzip File)

To unzip a zip file, we need to read the zip file with ZipInputStream and then read all the ZipEntry one by one. Then use FileOutputStream to write them to file system.

要解压缩一个zip文件,我们需要使用ZipInputStream读取该zip文件,然后逐个读取所有ZipEntry 。 然后使用FileOutputStream将它们写入文件系统。

We also need to create the output directory if it doesn’t exists and any nested directories present in the zip file.

如果输出目录不存在,并且zip文件中存在任何嵌套目录,我们还需要创建输出目录。

Here is the java unzip file program that unzips the earlier created tmp.zip to output directory.

这是一个Java解压缩文件程序,它将先前创建的tmp.zip解压缩到输出目录。

package com.journaldev.files;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;public class UnzipFiles {public static void main(String[] args) {String zipFilePath = "/Users/pankaj/tmp.zip";String destDir = "/Users/pankaj/output";unzip(zipFilePath, destDir);}private static void unzip(String zipFilePath, String destDir) {File dir = new File(destDir);// create output directory if it doesn't existif(!dir.exists()) dir.mkdirs();FileInputStream fis;//buffer for read and write data to filebyte[] buffer = new byte[1024];try {fis = new FileInputStream(zipFilePath);ZipInputStream zis = new ZipInputStream(fis);ZipEntry ze = zis.getNextEntry();while(ze != null){String fileName = ze.getName();File newFile = new File(destDir + File.separator + fileName);System.out.println("Unzipping to "+newFile.getAbsolutePath());//create directories for sub directories in zipnew File(newFile.getParent()).mkdirs();FileOutputStream fos = new FileOutputStream(newFile);int len;while ((len = zis.read(buffer)) > 0) {fos.write(buffer, 0, len);}fos.close();//close this ZipEntryzis.closeEntry();ze = zis.getNextEntry();}//close last ZipEntryzis.closeEntry();zis.close();fis.close();} catch (IOException e) {e.printStackTrace();}}}

Once the program finishes, we have all the zip file contents in the output folder with same directory hierarchy.

程序完成后,我们将所有zip文件内容存储在具有相同目录层次结构的输出文件夹中。

Output of the above program is:

上面程序的输出是:

Unzipping to /Users/pankaj/output/.DS_Store
Unzipping to /Users/pankaj/output/data/data.dat
Unzipping to /Users/pankaj/output/data/data.xml
Unzipping to /Users/pankaj/output/data/xmls/project.xml
Unzipping to /Users/pankaj/output/data/xmls/web.xml
Unzipping to /Users/pankaj/output/data.Xml
Unzipping to /Users/pankaj/output/DB.xml
Unzipping to /Users/pankaj/output/item.XML
Unzipping to /Users/pankaj/output/item.xsd
Unzipping to /Users/pankaj/output/ms/data.txt
Unzipping to /Users/pankaj/output/ms/project.doc

That’s all about java unzip file example.

这就是有关Java解压缩文件示例的全部内容。

翻译自: https://www.journaldev.com/960/java-unzip-file-example

java 上文件传示例

java 上文件传示例_Java解压缩文件示例相关推荐

  1. java图片上传下载_java web 文件上传与下载

    组件工作流程:WEB服务器request ServletFil eupLoad DiskFileItem Factory代表普通字段的FileItem代表上传文件1FileItem代表上传文件2Fil ...

  2. java 上传断点续传_JAVA大文件上传断点续传解决方案

    javaweb上传文件 上传文件的jsp中的部分 上传文件同样可以使用form表单向后端发请求,也可以使用 ajax向后端发请求 1.通过form表单向后端发送请求 Save 改进后的代码不需要for ...

  3. java web 上传附件_JAVA WEB文件上传步骤

    JAVA WEB文件上传步骤如下: 实现 Web 开发中的文件上传功能,两个操作:在 Web 页面添加上传输入项,在 Servlet 中读取上传文件的数据并保存在本地硬盘中. 1.Web 端上传文件. ...

  4. java图片上传下载_java实现文件的上传和下载

    1. servlet 如何实现文件的上传和下载? 1.1上传文件 参考自:http://blog.csdn.net/hzc543806053/article/details/7524491 通过前台选 ...

  5. ssm上传文件进度条_Java 单文件、多文件上传 / 实现上传进度条

    日常,工作 在这里总结一下上传吧(是以前做过的练习,就汇总到个人博客吧) java ssm 框架实现文件上传 实现:单文件上传.多文件上传(单选和多选),并且用 ajax 异步刷新,在当前界面显示上传 ...

  6. java 文件随机读取_Java 实现文件随机读写-RandomAccessFile

    现有如下的一个需求,向已存在1G数据的txt文本里末尾追加一行文字,内容如下"Lucene是一款非常优秀的全文检索库".可能大多数朋友会觉得这个需求很easy,说实话,确实easy ...

  7. 自己用java实现飞鸽传书 2 - 实现文件传输

    第二步:实现文件传递. 上一步只是从服务端传递了一个字符串到客户端,这次需要对代码进行调整,实现从服务端获取文件,在客户端将文件存入目标地址. 调整后的代码: 服务端: import java.io. ...

  8. java遍历文件和归类_java读取文件的两种方法:java.io和java.lang.ClassLoader

    java读取文件的两种方法:java.io和java.lang.ClassLoader 什么时候使用java.io,什么时候使用java.lang.ClassLoader呢? (注:要是之前读xml文 ...

  9. java压缩zip文件夹错误_Java将文件或者文件夹压缩成zip(修复文件夹中存在多个文件报Stream Closed错误问题)...

    项目场景: Java将文件或者文件夹压缩成zip(修复文件夹中存在多个文件报Stream Closed错误问题) 问题描述: 最近的项目需要将多级文件夹压缩成zip,网上找了几个工具类,都会报错,所以 ...

最新文章

  1. Libgcrypt实现AES加密
  2. ArcGIS API for Silverlight开发
  3. [JAVA]引入目录下所有jar包等问题
  4. 深度解析dba_segments和sys.seg$中的细节差异(下)
  5. csdn自定义模块backup
  6. 日志组件logback介绍及配置使用方法
  7. 七、Go 语言面向对象编程
  8. 基于AVS2的图片容器——TPG:现状与改进之路
  9. 数据库事务及事务的特征
  10. C语言深入理解系列 - 关键字:auto和register
  11. EntityFramework 如何查看执行的 SQL 代码?
  12. C#AJAX 文件上传
  13. 一个优质软件测试工程师简历的范文(答应我一定要收藏起来)
  14. 在React中使用Shadow DOM
  15. google手机连接wifi后提示“无法连接互联网“的原因和解决方法
  16. 个人推荐一款并发测试工具
  17. [暑期实训] 任务记录 2021-06-29
  18. wifi 小程序 透传_微信小程序实现的一键连接wifi功能示例
  19. JMockit 和 junit 使用出现异常 JMockit wasn't properly initialized 解决
  20. 给Div添加边框颜色

热门文章

  1. 一看就懂ReactJS
  2. 【转】深入理解JVM—JVM内存模型
  3. 在 Linux 上部署 Django 应用,nginx+gunicorn+supervisor
  4. [转载] python基础知识三——try与except处理异常语句
  5. [转载] python猜字谜游戏_Python Hangman猜字游戏
  6. [转载] 使用openpyxl模块向Excel中插入图片
  7. [转载] 1.1.1 Python常用的数学函数
  8. SPI通信实验---verilog(FPGA作为从机,使用可读可写)
  9. vue nextTick深入理解-vue性能优化、DOM更新时机、事件循环机制
  10. 关于SpringBoot和Thymeleaf模板中遇到的问题