首页 新闻 会员 周边 捐助

求解ZIP压缩时出现的困惑😁

-1
[已解决问题] 解决于 2018-02-14 17:25

RT,写Zip压包时发现如果文件夹中有空文件夹,不知道如何打压了.所不太清楚,还请大神看看我代码:

package com.bingco.main;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * ZIP文档读取与创建
 */
public class Main {

    public static void main(String[] args) {
        new Main().method_4();
    }

    private void method_1() {
        ZipInputStream zipIsm = null;
        try {
            //创建输入流对象
            zipIsm = new ZipInputStream(Files.newInputStream(Paths.get("C:\\Users\\bingco\\Desktop\\bingco.txt")));
            // 获取压缩流中的条目
            ZipEntry entry;
            while ((entry = zipIsm.getNextEntry()) != null) {
                System.out.println(entry.getName());
                // 关闭当前条目的子流
                zipIsm.closeEntry();
            }
            if (entry == null) System.out.println("This is not ZIP file or file size is zero!");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭主流
            try {
                if(zipIsm != null) zipIsm.close();
            } catch (IOException e) {
            }
        }

    }

    private void method_2() {
        ZipInputStream zipIsm = null;
        try {
            zipIsm = new ZipInputStream(Files.newInputStream(Paths.get("C:\\Users\\bingco\\Desktop\\bingco.zip")));
            /*
             * 证明它是递归式的迭代压缩文件目录.    如果迭代到一个文件,那么获取的条目名称是路径加上文件名!
             */
            ZipEntry entry;

            while((entry = zipIsm.getNextEntry()) != null) {
                if (entry.isDirectory()) System.out.println("文件夹: " + entry.getName());
                else System.out.println("文件: " + entry.getName());
                zipIsm.closeEntry();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(zipIsm != null) zipIsm.close();
            } catch (IOException e) {
            }
        }
    }

    private void method_3() {
        ZipInputStream zipIsm = null;
        BufferedOutputStream osm = null;
        /*
         * 代码解压缩ZIP文件
         */
        try {
            zipIsm = new ZipInputStream(Files.newInputStream(Paths.get("C:\\Users\\bingco\\Desktop\\bingco.zip")));
            File file = new File("C:\\Users\\bingco\\Desktop\\bingco-zip");
            if (!file.exists()) file.mkdirs();

            ZipEntry entry;
            while ((entry = zipIsm.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    File f = new File(file, entry.getName());
                    if (!f.exists()) f.mkdirs();
                }

                else {
                    osm = new BufferedOutputStream(new FileOutputStream(new File(file, entry.getName())));
                    int len;
                    byte[] by = new byte[1024];
                    while((len = zipIsm.read(by, 0, by.length)) > 0) {
                        osm.write(by, 0, len);
                    }
                    zipIsm.closeEntry();
                    osm.close();
                }
            }
        }

        catch (IOException e) {
            e.printStackTrace();
        }

        finally {
            try {
                if(zipIsm != null) zipIsm.close();
                if(osm != null) osm.close();
            }
            catch (IOException e) {}
        }
    }

    private void method_4() {
        ZipOutputStream zipOsm = null;
        File file = new File("C:\\Users\\bingco\\Desktop\\bingco");
        if (!file.exists()) {
            System.out.println("undefined the file or folder !");
            return;
        }

        try {
            zipOsm = new ZipOutputStream(new FileOutputStream("C:\\Users\\bingco\\Desktop\\" + file.getName() + ".zip"));
            String parent = file.getParent() + "\\";
            zipPack(file, parent, zipOsm);
        }

        catch(IOException e) {
            e.printStackTrace();
        }

        finally {
            try {
                if (zipOsm != null) zipOsm.close();
            }
            catch (IOException e) {}
        }
    }

    private void zipPack(File file, String parent, ZipOutputStream zipOsm) throws IOException {
        if (file.isFile()) {
            String entryName = file.getPath().replace(parent, "");
            zipOsm.putNextEntry(new ZipEntry(entryName));

            BufferedInputStream ism = new BufferedInputStream(new FileInputStream(file.getPath()));
            try {
                int len;
                byte by[] = new byte[1024];
                while ((len = ism.read(by, 0, by.length)) > 0) {
                    zipOsm.write(by, 0, len);
                }
                zipOsm.closeEntry();
            }

            catch (IOException e) {
                throw new RuntimeException(e);
            }

            finally {
                if (ism != null) ism.close();
            }
        }
        else {
            for (File fe : file.listFiles()) zipPack(fe, parent, zipOsm);
        }
    }
}
bingco的主页 bingco | 菜鸟二级 | 园豆:213
提问于:2018-02-12 23:11
< >
分享
最佳答案
1

 遍历文件,判断是否是文件夹还是文件,是文件夹判断文件夹下是否有内容,如果没有直接写入路径,有则递归。

    public void zipDistory(String name,File file,ZipOutputStream zipOut) throws IOException{
        File[] f=file.listFiles();
        for(File e:f ){
            if(e.isDirectory()){
                if(e.listFiles().length==0){
                    zipOut.putNextEntry(new ZipEntry(file.getName()+File.separator+e.getName()));
                    zipOut.closeEntry();
                }else{
                zipDistory(name+File.separator+file.getName()+file.separator+e.getName(), e,zipOut);
                }
            }else{
                try {
                    BufferedInputStream buff=new BufferedInputStream(new FileInputStream(e));
                    zipOut.putNextEntry(new ZipEntry(name+File.separator+file.getName()+File.separator+e.getName()));
                    byte[] b=new byte[buff.available()];
                    buff.read(b);
                    buff.close();
                    zipOut.write(b,0,b.length);
                } catch (FileNotFoundException e1) {
                    logger.info("文件读取失败");
                    e1.printStackTrace();
                }
            }
        }
    }
奖励园豆:5
CXL123 | 初学一级 |园豆:90 | 2018-02-13 14:28

原来如此,关键在于  File.separator (系统分隔符), 谢谢解惑

bingco | 园豆:213 (菜鸟二级) | 2018-02-14 17:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册