集册 Java实例教程 创建/更新一个zip文件。

创建/更新一个zip文件。

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

503
创建/更新一个zip文件。
/**
 from
* 时代Java公众号 - nowjava.com 
**/


//package com.nowjava;


import java.io.IOException;

import java.net.URI;


import java.nio.file.FileSystem;

import java.nio.file.FileSystems;

import java.nio.file.FileVisitResult;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.SimpleFileVisitor;

import java.nio.file.StandardCopyOption;
/** 
来 自 
时 代      J a v a   公   众 号 - nowjava.com
**/

import java.nio.file.attribute.BasicFileAttributes;


import java.util.HashMap;

import java.util.Map;


public class Main {

    /**

     * Creates/updates a zip file.

     * 

     * @param zipFilename the name of the zip to create

     * @param filenames list of filename to add to the zip

     * @throws IOException

     */

    public static void create(String zipFilename, String... filenames)

            throws IOException {

        try (FileSystem zipFileSystem = createZipFileSystem(zipFilename,

                true)) {

            final Path root = zipFileSystem.getPath("/");


            //iterate over the files we need to add

            for (String filename : filenames) {

                final Path src = Paths.get(filename);


                //add a file to the zip file system

                if (!Files.isDirectory(src)) {

                    final Path dest = zipFileSystem.getPath(

                            root.toString(), src.toString());

                    final Path parent = dest.getParent();

                    if (Files.notExists(parent)) {

                        System.out

                                .printf("Creating directory %s\n", parent);

                        Files.createDirectories(parent);

                    }

                    Files.copy(src, dest,

                            StandardCopyOption.REPLACE_EXISTING);

                } else {

                    //for directories, walk the file tree

                    Files.walkFileTree(src, new SimpleFileVisitor<Path>() {

                        @Override

                        public FileVisitResult visitFile(Path file,

                                BasicFileAttributes attrs)

                                throws IOException {

                            final Path dest = zipFileSystem.getPath(

                                    root.toString(), file.toString());

                            Files.copy(file, dest,

                                    StandardCopyOption.REPLACE_EXISTING);

                            return FileVisitResult.CONTINUE;

                        }


                        @Override

                        public FileVisitResult preVisitDirectory(Path dir,

                                BasicFileAttributes attrs)

                                throws IOException {

                            final Path dirToCreate = zipFileSystem.getPath(

                                    root.toString(), dir.toString());

                            if (Files.notExists(dirToCreate)) {

                                System.out.printf(

                                        "Creating directory %s\n",

                                        dirToCreate);

                                Files.createDirectories(dirToCreate);

                            }

                            return FileVisitResult.CONTINUE;

                        }

                    });

                }

            }

        }

    }


    
展开阅读全文