简体   繁体   English

Java获取Zip文件内容

[英]Java Get Zip file content

My problem is that I want to see if a file is in a zip file. 我的问题是我想查看文件是否在zip文件中。 So I have made this code : 所以我做了这段代码:

File zipf = new File(backupFolder, dfws.format(new Date()) + ".zip");
                        if (!zipf.exists()) zipf.createNewFile();
                        fs = new FileOutputStream(zipf);
                        ZipOutputStream zos = new ZipOutputStream(fs);
                        System.out.println("size : " + zipf.length());
                        if (zipf.length() > 0) {
                            ZipFile zf = new ZipFile(zipf);

                            System.out.println("entry : " + zf.getEntry(name));
                            if (zf.getEntry(name) != null) {
                                int index = 1;
                                while (zf.getEntry(index + "_" + name) != null) {
                                    index++;
                                }
                                name = index + "_" + name;
                            }

                            zf.close();
                        }
                        System.out.println("index found : " + name);

But the problem is that the length of the file is always 0. And I can't create an instance of ZipFile if the zip file doesn't have files inside. 但是问题是文件的长度始终为0。而且如果zip文件中没有文件,则无法创建ZipFile的实例。

感谢RealSkeptic:创建ZipFile之后,我不得不打开FileOutputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM