Java 获取 Zip 文件内容

Java Get Zip file content

我的问题是我想查看一个文件是否在 zip 文件中。所以我做了这个代码:

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);

但问题是文件的长度始终为 0。如果 zip 文件中没有文件,我将无法创建 ZipFile 的实例。

感谢 RealSkeptic:我必须在创建 ZipFile 后打开 FileOutputStream。