java 上文件传示例_Java解压缩文件示例

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 exist
        if(!dir.exists()) dir.mkdirs();
        FileInputStream fis;
        //buffer for read and write data to file
        byte[] 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 zip
                new 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 ZipEntry
                zis.closeEntry();
                ze = zis.getNextEntry();
            }
            //close last ZipEntry
            zis.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 上文件传示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值