即使我收到正确的名称和路径

时间:2016-05-19 06:48:48

标签: java delete-file

我想在解压缩后删除一个zip文件。但我无法删除它:

if (file.getName().contains(".zip")) {
    System.out.println(file.getAbsolutePath()); // I am getting the correct path
    file.delete();
    System.out.println(file.getName()); // I am getting the correct name Script-1.zip
}

这是完整的代码

public class Zip4 {

    public static void main(String[] args) {
        File[] files = new File(args[0]).listFiles();

        for(File file : files)
        //  System.out.println(file.getName());
            //if(file.getName().contains("1400") && file.getName().contains(".zip"))
              extractFolder(args[0] + file.getName(), args[1]);
        DeleteFiles();

     //   for(File file : files)
                //  System.out.println("File:C:/1/"+ file.getName());

//      extractFolder(args[0]+file.getName(),args[1]);

    }

    private static void DeleteFiles()
    {
        File f = null;
        File[] paths;
        f = new File("D:/Copyof");
        paths = f.listFiles();

          for(File path:paths)
             {
                // prints file and directory paths
                if(path.getName().contains("J14_0_0RC") || path.getName().contains(".zip") || path.getName().contains(".log"))
                {

                     //System.out.println(path);
                     path.delete();
                }

             }
    }

    private static void extractFolder(String zipFile,String extractFolder) 
    {
        try
        {
            int BUFFER = 2048;
            File file = new File(zipFile);
            ZipFile zip = new ZipFile(file);
            String newPath = extractFolder;

            new File(newPath).mkdir();
            Enumeration zipFileEntries = zip.entries();

            // Process each entry
            while (zipFileEntries.hasMoreElements())
            {
                // grab a zip file entry
                ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
                String currentEntry = entry.getName();

                File destFile = new File(newPath, currentEntry);
                //destFile = new File(newPath, destFile.getName());
                File destinationParent = destFile.getParentFile();

                // create the parent directory structure if needed
                destinationParent.mkdirs();

                if (!entry.isDirectory())
                {
                    BufferedInputStream is = new BufferedInputStream(zip
                    .getInputStream(entry));
                    int currentByte;
                    // establish buffer for writing file
                    byte data[] = new byte[BUFFER];

                    // write the current file to disk
                    FileOutputStream fos = new FileOutputStream(destFile);
                    BufferedOutputStream dest = new BufferedOutputStream(fos,
                    BUFFER);

                    // read and write until last byte is encountered
                    while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, currentByte);
                    }
                    dest.flush();
                    dest.close();
                    fos.flush();
                    fos.close();
                    is.close();
                }

            }

            if(file.getName().contains(".zip"))
            {
                System.out.println(file.getAbsolutePath());
                file.delete();
                  System.out.println(file.getName());
            }
        }
        catch (Exception e) 
        {
            System.out.println("Error: " + e.getMessage());

        }
    }
}

1 个答案:

答案 0 :(得分:2)

ZipFile是一个可以关闭的资源。所以,只要你在finally块中完成它就可以close()或者用try-with-resources创建它(从java7开始):

try(ZipFile zip = new ZipFile(file)){
  //unzip here
}
file.delete();

除此之外,你应该重新审视这个块

dest.flush();
dest.close();
fos.flush();
fos.close();
is.close();

很容易发生资源泄漏。如果其中一个上层调用失败,则不会调用所有后续调用,从而导致未封闭的资源和资源泄漏。

最好也是在这里使用try-with-resources

try(BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
    FileOutputStream fos = new FileOutputStream(destFile);
    BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
  //write the data
} //all streams are closed implicitly here

或者使用现有工具,例如Apache Commons IO IOUtil.closeQuietly(resource)或将每个调用嵌入

if(resource != null) {
  try{
     resource.close();
  } catch(IOException e){
     //omit
  }
}

您还可以省略对flush()的调用,这在关闭资源时会隐式执行。