集册 Java实例教程 解压缩文件

解压缩文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

501
解压缩文件

import java.io.BufferedInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.nio.file.FileSystem;/** 来自 时代Java - nowjava.com**/

import java.nio.file.FileSystems;

import java.nio.file.Files;

import java.nio.file.Path;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;


public class Main {

  public static void main(String[] args) throws Exception {

    ZipFile file = new ZipFile("file.zip");

    FileSystem fileSystem = FileSystems.getDefault();
    /*
     from 时 代      J a v a   公   众 号 - nowjava.com 
    */

    Enumeration<? extends ZipEntry> entries = file.entries();

    String uncompressedDirectory = "uncompressed/";

    Files.createDirectory(fileSystem.getPath(uncompressedDirectory));

    while (entries.hasMoreElements()) {

      ZipEntry entry = entries.nextElement();

      if (entry.isDirectory()) {

        System.out.println("Creating Directory:" + uncompressedDirectory

            + entry.getName());

        Files.createDirectories(fileSystem.getPath(uncompressedDirectory

            + entry.getName()));

      } else {

        InputStream is = file.getInputStream(entry);

        System.out.println("File :" + entry.getName());

        BufferedInputStream bis = new BufferedInputStream(is);


        String uncompressedFileName = uncompressedDirectory + entry.getName();

        Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);

        Files.createFile(uncompressedFileP
展开阅读全文