2016-05-19 3 views
0

解凍後にZIPファイルを削除しようとしています。しかし、私はそれを削除することはできませんよ。正しい名前とパスを取得しても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()); 

     } 
    } 
} 
+0

ファイルが '.ZIP'で終わっている可能性があります。そして 'contains(" .zip "))'は期待どおりに動作しません。 – SubOptimal

+0

**最後にblcckで開いたすべてのリソースを閉じます** – emotionlessbananas

答えて

2

ZipFileは閉鎖可能な資源です。だから、どちらかclose()それをあなたがで完了したら、最後にブロックしたり(java7以降)try-with-resourcesでそれを作成します。

これとは別に
try(ZipFile zip = new ZipFile(file)){ 
    //unzip here 
} 
file.delete(); 

、あなたは非常にがちである。このブロック

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

を再訪する必要がありリソースリークにつながります。上位の呼び出しの1つが失敗した場合、後続の呼び出しはすべて呼び出されず、その結果、閉じられていないリソースとリソースのリークが発生します。

ここでも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 IOIOUtil.closeQuietly(resource)かのために

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

にすべての単一の呼び出しをembeddまた、リソースを閉じるときに暗黙的に行われているflush()への呼び出しを省略することができます。

+0

私はJavaでうまくいかず、 'Zipfile.close();'を追加しようとしましたが、 '非staticメソッドclose ()型ZipFile'から私はそれを解決する方法を知りませんでした。試しを追加するとあなたのアドバイスに従って、私のコードではっきりしないだろうか? – Moudiz

+0

あなたが作成した 'ZipFile'のインスタンス - >' zip.close() 'ですが、java> = 7の場合はtry-with-resourcesを使う方が良いので、それを全く心配する必要はありません –

関連する問題