java zip 文件夹

Today we will look into java zip file example. We will also compress a folder and create zip file using java program.

今天,我们将研究java zip文件示例。 我们还将使用Java程序压缩文件夹并创建zip文件。

Java ZIP (Java ZIP)

java.util.zip.ZipOutputStream can be used to compress a file into ZIP format. Since a zip file can contain multiple entries, ZipOutputStream uses java.util.zip.ZipEntry to represent a zip file entry.

java.util.zip.ZipOutputStream可用于将文件压缩为ZIP格式。 由于一个zip文件可以包含多个条目,因此ZipOutputStream使用java.util.zip.ZipEntry表示一个zip文件条目。

Java ZIP文件 (Java ZIP File)

Creating a zip archive for a single file is very easy, we need to create a ZipOutputStream object from the FileOutputStream object of destination file. Then we add a new ZipEntry to the ZipOutputStream and use FileInputStream to read the source file to ZipOutputStream object. Once we are done writing, we need to close ZipEntry and release all the resources.

为单个文件创建一个zip存档非常容易,我们需要从目标文件的FileOutputStream对象创建一个ZipOutputStream对象。 然后,我们向ZipOutputStream添加一个新的ZipEntry,并使用FileInputStream将源文件读取到ZipOutputStream对象。 完成编写后,我们需要关闭ZipEntry并释放所有资源。

Java Zip文件夹 (Java Zip Folder)

Zipping a directory is little tricky, first we need to get the files list as absolute path. Then process each one of them separately. We need to add a ZipEntry for each file and use FileInputStream to read the content of the source file to the ZipEntry corresponding to that file.

压缩目录有点棘手,首先我们需要获取文件列表作为绝对路径。 然后分别处理每个。 我们需要为每个文件添加一个ZipEntry,并使用FileInputStream将源文件的内容读取到与该文件相对应的ZipEntry。

Java Zip示例 (Java Zip Example)

Here is the java program showing how to zip a single file or zip a folder in java.

这是显示如何在Java中压缩单个文件或压缩文件夹的Java程序。

package com.journaldev.files;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class ZipFiles {List<String> filesListInDir = new ArrayList<String>();public static void main(String[] args) {File file = new File("/Users/pankaj/sitemap.xml");String zipFileName = "/Users/pankaj/sitemap.zip";File dir = new File("/Users/pankaj/tmp");String zipDirName = "/Users/pankaj/tmp.zip";zipSingleFile(file, zipFileName);ZipFiles zipFiles = new ZipFiles();zipFiles.zipDirectory(dir, zipDirName);}/*** This method zips the directory* @param dir* @param zipDirName*/private void zipDirectory(File dir, String zipDirName) {try {populateFilesList(dir);//now zip files one by one//create ZipOutputStream to write to the zip fileFileOutputStream fos = new FileOutputStream(zipDirName);ZipOutputStream zos = new ZipOutputStream(fos);for(String filePath : filesListInDir){System.out.println("Zipping "+filePath);//for ZipEntry we need to keep only relative file path, so we used substring on absolute pathZipEntry ze = new ZipEntry(filePath.substring(dir.getAbsolutePath().length()+1, filePath.length()));zos.putNextEntry(ze);//read the file and write to ZipOutputStreamFileInputStream fis = new FileInputStream(filePath);byte[] buffer = new byte[1024];int len;while ((len = fis.read(buffer)) > 0) {zos.write(buffer, 0, len);}zos.closeEntry();fis.close();}zos.close();fos.close();} catch (IOException e) {e.printStackTrace();}}/*** This method populates all the files in a directory to a List* @param dir* @throws IOException*/private void populateFilesList(File dir) throws IOException {File[] files = dir.listFiles();for(File file : files){if(file.isFile()) filesListInDir.add(file.getAbsolutePath());else populateFilesList(file);}}/*** This method compresses the single file to zip format* @param file* @param zipFileName*/private static void zipSingleFile(File file, String zipFileName) {try {//create ZipOutputStream to write to the zip fileFileOutputStream fos = new FileOutputStream(zipFileName);ZipOutputStream zos = new ZipOutputStream(fos);//add a new Zip Entry to the ZipOutputStreamZipEntry ze = new ZipEntry(file.getName());zos.putNextEntry(ze);//read the file and write to ZipOutputStreamFileInputStream fis = new FileInputStream(file);byte[] buffer = new byte[1024];int len;while ((len = fis.read(buffer)) > 0) {zos.write(buffer, 0, len);}//Close the zip entry to write to zip filezos.closeEntry();//Close resourceszos.close();fis.close();fos.close();System.out.println(file.getCanonicalPath()+" is zipped to "+zipFileName);} catch (IOException e) {e.printStackTrace();}}}

Output of the above java zip example program is:

上面的Java zip示例程序的输出为:

/Users/pankaj/sitemap.xml is zipped to /Users/pankaj/sitemap.zip
Zipping /Users/pankaj/tmp/.DS_Store
Zipping /Users/pankaj/tmp/data/data.dat
Zipping /Users/pankaj/tmp/data/data.xml
Zipping /Users/pankaj/tmp/data/xmls/project.xml
Zipping /Users/pankaj/tmp/data/xmls/web.xml
Zipping /Users/pankaj/tmp/data.Xml
Zipping /Users/pankaj/tmp/DB.xml
Zipping /Users/pankaj/tmp/item.XML
Zipping /Users/pankaj/tmp/item.xsd
Zipping /Users/pankaj/tmp/ms/data.txt
Zipping /Users/pankaj/tmp/ms/project.doc

Notice that while logging files to zip in directory, I am printing absolute path. But while adding zip entry, I am using relative path from the directory so that when we unzip it, it will create the same directory structure. That’s all for Java zip example.

请注意,在将文件记录到zip目录中的同时,我正在打印绝对路径。 但是,在添加zip条目时,我使用的是目录的相对路径,因此当我们将其解压缩时,它将创建相同的目录结构。 这就是Java zip示例的全部内容。

翻译自: https://www.journaldev.com/957/java-zip-file-folder-example

java zip 文件夹

java zip 文件夹_Java Zip文件文件夹示例相关推荐

  1. java zip文件加密_java自动压缩文件并加密

    实现功能:自动压缩并加密 /** * * @Title: zipFilesAndEncrypt * @Description: 将指定路径下的文件压缩至指定zip文件,并以指定密码加密,若密码为空,则 ...

  2. java 压缩文件夹_java 实现压缩文件(单文件 或 文件夹)

    接着上篇了解一下java压缩实现过程,下面的是支持 单文件 或 文件夹 压缩的实现,使用递归. 效果: 代码: package com.gx.compress; import java.io.Buff ...

  3. java文件递归_java递归处理文件夹和文件

    import java.io.File; /** * 文件综合使用示例 */ public class FileDelete { public static void main(String[] ar ...

  4. java 文件 递归_JAVA实现遍历文件夹下的所有文件(递归调用和非递归调用)

    JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...

  5. java多线程 文件夹_Java多线程遍历文件夹,广度遍历加多线程加深度遍历结合

    复习IO操作,突然想写一个小工具,统计一下电脑里面的Java代码量还有注释率,最开始随手写了一个递归算法,遍历文件夹,比较简单,而且代码层次清晰,相对易于理解,代码如下:(完整代码贴在最后面,前面是功 ...

  6. java删除相对路径文件夹_Java IO,io,文件操作,删除文件,删除文件夹,获取文件父级目录...

    Java IO,io,文件操作,删除文件,删除文件夹,获取文件父级目录 这里先简单的贴下常用的方法: File.separator //当前系统文件分隔符 File.pathSeparator// F ...

  7. java 项目文件夹_java项目三大文件夹的区别(package,source folder,folder)

    转载自 慢吞吞. 三大文件夹就好像Windows的文件夹的作用类似.通过树目录区别不同的文件,作用就是对文件进行管理. Package(包):当你在建立一个package时,它自动建立到source ...

  8. java递归删除空文件夹_Java 删除空文件夹和文件夹及其下面的文件

    一.删除代码: package deletedir; import java.io.File; public class DeleteDir { //删除空目录 public void doDelet ...

  9. java读取文件夹_Java读取某个文件夹下的所有文件(支持多级文件夹)

    packagecom.vocy.water.batch;importjava.io.FileNotFoundException;importjava.io.IOException;importjava ...

最新文章

  1. SkFlattenable /Registrar/
  2. [恢]hdu 2117
  3. java学习体会论文_Java I/O学习心得一
  4. MySQL数据库存入日期(java.sql.Date)数据,天数会少一天的问题
  5. 人工智能在语音和数字图像处理领域有哪些具体化应用_智能呼叫中心系统有哪些优势...
  6. Java 设置文件只读
  7. GoEasy小程序即时通讯源码 v1.1.0基于GoEasy提供的websocket通讯服务
  8. MD5算法之C#程序 MD5算法描述
  9. xp 挂linux上网,XP系统挂载Linux NFS共享
  10. 一次redis集群连接数占满问题的排查
  11. 使用SQLite3支持中文路径
  12. 深度数据对接 链接服务器 数据传输
  13. Git常用命令(持续更新)
  14. 正点原子STM32串口例程解析
  15. MATLAB含有绝对值的线性规划,Lingo求解带绝对值的线性规划模型
  16. 手机通过外网(HFS)访问电脑文件
  17. 修改联通服务器,联通光猫dns设置服务器地址
  18. 掌握搜索引擎优化方法使关键词快速排名
  19. steam for linux 安装目录,我该如何安装Steam?
  20. Authorization Basic认证 笔记

热门文章

  1. 使php支持pdo_mysql
  2. 修车-最小费用最大流
  3. Alfred Remote初体验
  4. [转载] numpy教程:排序、搜索和计数
  5. [转载] 动态口令,动态密码生成(OTP)
  6. 四元数运动学笔记(1)旋转的表示
  7. 2015 圣诞 限免软件分享
  8. ExtJS视频学习笔记
  9. nopcommerce笔记3 还可以控制什么
  10. 用shell求两个文件的差集