首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >即使我得到正确的名称和路径,也不会删除Zip文件。

即使我得到正确的名称和路径,也不会删除Zip文件。
EN

Stack Overflow用户
提问于 2016-05-19 06:48:47
回答 1查看 1K关注 0票数 0

我正在尝试删除一个压缩文件后,解压。但我无法删除:

代码语言:javascript
代码运行次数:0
运行
复制
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
}

这是完整的代码

代码语言:javascript
代码运行次数:0
运行
复制
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());

        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-19 06:59:56

ZipFile是一个可关闭的资源。所以,当您在一个finally块中完成时,要么使用close()创建它,要么使用try-with-resources (自java7)创建它:

代码语言:javascript
代码运行次数:0
运行
复制
try(ZipFile zip = new ZipFile(file)){
  //unzip here
}
file.delete();

除此之外,你还应该重新审视这个街区。

代码语言:javascript
代码运行次数:0
运行
复制
dest.flush();
dest.close();
fos.flush();
fos.close();
is.close();

这很容易导致资源泄漏。如果一个上级调用失败,则不会调用所有后续调用,从而导致未关闭的资源和资源泄漏。

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

代码语言:javascript
代码运行次数:0
运行
复制
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)或将每个调用嵌入到

代码语言:javascript
代码运行次数:0
运行
复制
if(resource != null) {
  try{
     resource.close();
  } catch(IOException e){
     //omit
  }
}

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37315806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档