集册 Java实例教程 创建邮政编码档案

创建邮政编码档案

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

448
创建邮政编码档案

import java.io.*;
/*
时   代     Java  公  众  号 - nowjava.com
*/

import java.util.HashMap;

import java.util.Map;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;


public class CreateZipArchive {

    public static void main(String[] args) {

        HashMap<String,File> files = new HashMap<String, File>() {{

            put("count-chars.txt",new File("files/count-chars.txt"));

            put("words.txt",new File("files/words.txt"));

            put("lines.txt",new File("files/lines.txt"));
            /**
            时 代 J a v a 公 众 号 - nowjava.com
            **/

        }};

        zipFiles(files,"files/text-files.zip");

    }


    public static void zipFiles(Map<String,File> files,String archiveName) {

        try(ZipOutputStream destination = new ZipOutputStream(new FileOutputStream(archiveName))){

            for(Map.Entry<String,File> file : files.entrySet()){

                try(FileInputStream source = new FileInputStream(file.getValue())){

                    destination.putNextEntry(new ZipEntry(file.getKey()));


                    byte[] buffer = new byte[4096];

                    while(true){

                        int readed = source.read(buffer,0,buffer.length);

                        
展开阅读全文