删除 java 中的目录不是删除目录

Delete directory in java is not deleting directory

压缩后我正在删除目录。我使用下面的代码来压缩和删除。

我可以压缩但无法删除文件夹。

任何人都可以指出我哪里做错了。

这是我正在使用的代码

    public class ZipDirectory {

        public static void main(String[] a) throws Exception {
            zipFolder("d:\conf2", "d:\conf2.zip");
          }

          static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
            ZipOutputStream zip = null;
            FileOutputStream fileWriter = null;
            fileWriter = new FileOutputStream(destZipFile);
            zip = new ZipOutputStream(fileWriter);

            addFolderToZip("", srcFolder, zip);
            zip.flush();
            zip.close();

            delete(new File(srcFolder));
          }

          static private void addFileToZip(String path, String srcFile, ZipOutputStream zip)
              throws Exception {

            File folder = new File(srcFile);
            if (folder.isDirectory()) {
              addFolderToZip(path, srcFile, zip);
            } else {
              byte[] buf = new byte[1024];
              int len;
              FileInputStream in = new FileInputStream(srcFile);
              zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
              while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
              }
            }
          }

          static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
              throws Exception {
            File folder = new File(srcFolder);
            for (String fileName : folder.list()) {       
                addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);        
            }
          }

          static private void delete (File path){
            if( path.exists() ) {
                File[] files = path.listFiles();
                for(int i=0; i<files.length; i++) {
                     files[i].delete();                
                }
              }
            path.delete();
          }   

        }

请关闭 FileInputStream 的实例以使您的删除成功。

请在addFileToZip()方法中添加in.close()

您没有收集您创建的 ZipOutputStream 对象。尝试在您的代码中替换以下方法。在这里,我在 try-with-resource 中创建了 ZipOutputStrean 对象并且它有效

static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
    try(ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(destZipFile)))
    {
        addFolderToZip("", srcFolder, zip);
        zip.flush();
        zip.close();

        delete(new File(srcFolder));
    }
}

跟踪删除方法后显示如下

07:58:12.734018754 0x2970500 mt.0 入口 >java/io/File.delete()Z 字节码方法,This = 0xfffc4810

07:58:12.734019108 0x2970500 mt.3 条目 >java/lang/System.getSecurityManager()Ljava/lang/SecurityManager;字节码静态方法

07:58:12.734019462 0x2970500 mt.9 退出

07:58:12.734019815 0x2970500 mt.0 入口 >java/io/File.isInvalid()Z 字节码方法,This = 0xfffc4810

On deleting the file, security manager refuse to delete the file because of already existing file descriptor associated to that. Close the Fileinput stream to avoid this condition.