2017-01-13 1 views
0

일부 파일이 들어있는 폴더가 있습니다. 이제는 이미 존재하는 지퍼에 추가하려고합니다. zip에 추가 할 파일이 이미 있으면 이전 파일을 새 파일로 바꿉니다. zip 작업의 경우 zip4j jar를 사용하고 있습니다. 이것은 내 코드자바에서 지퍼 파일을 지우는 중

 for(File entry : temp.listFiles()) 
     { 
      String file = entry.getName(); 

      if(trgZip.getFileHeader(file) != null) 
      { 
       trgZip.removeFile(file); 
      } 
      ZipParameters param = new ZipParameters(); 
      trgZip.addFile(entry, param); 
     } 

의 조각 그러나 나는이 예외 net.lingala.zip4j.exception.ZipException 무엇입니까 : 오래된 zip 파일을 삭제할 수 없습니다 아무도 내가이 문제를 해결하기 위해 무엇을해야하는지 제안 해주십시오 수 있습니다 , 또는 내가 잘못 가고있는 곳, 또는 어떻게하면 removeFile 메서드가 작동하기 때문에 오류 지점을 찾아 볼 수 있습니다.

미리 감사드립니다.

+1

가능한 중복 http://stackoverflow.com/questions/5244963/delete-files-from-a-zip-archive-without -decompressing-in-java-or-maybe-python) – Raedwald

답변

0

시도해주세요 !! zip 파일의 경로를 첫 번째 인수로, zip 파일에서 두 번째 인수로 삭제할 파일 이름을 입력하십시오.

public static void deleteFile(String zipFilePath,String fileName) throws Exception{ 
     Map<String, String> zip_properties = new HashMap<>(); 
     zip_properties.put("create", "false"); 

     /* Specify the path to the ZIP File that you want to read as a File System */ 
     URI zip_disk = URI.create("jar:file:"+zipFilePath); 

     /* Create ZIP file System */ 
     try (FileSystem zipfs = FileSystems.newFileSystem(zip_disk, zip_properties)) { 
      /* Get the Path inside ZIP File to delete the ZIP Entry */ 
      Path pathInZipfile = zipfs.getPath(fileName); 
      System.out.println("About to delete an entry from ZIP File" + pathInZipfile.toUri()); 
      /* Execute Delete */ 
      Files.delete(pathInZipfile); 
      System.out.println("File successfully deleted"); 
     } 
    } 
([Java 또는 어쩌면 파이썬에서 압축 풀기없이 ZIP 아카이브에서 파일을 삭제]의
+0

FileSystems를 사용해 보았지만 제대로 작동하지 못했습니다. 그것은 ** ReadOnlyFileSystems ** Exception 에러를 보냈습니다. zip4j로 무엇이든 제안하거나,이 FilesSystem을 어떻게 작동하게 할 수 있습니까? – dopeE

+0

시도한 코드와 전체 스택 추적을 공유하십시오. –

+0

사용 ** java.nio.file.FileSystem **이 아닌 FileSystem 또는 FileSystems –

관련 문제