2014-04-07 2 views
2

음 .2 .xml 파일을 생성하고 있지만 폴더에 저장하고 압축하여 생성하고 싶지 않습니다. 별도로 폴더에서 분리되어 압축됩니다. d 당신은 그것을 얻었습니까?코드에서 생성 된 파일을 압축하는 방법 (Java)

나는 두 파일을 폴더에 넣고 집어 넣기를 원합니다. 저장하려는 유일한 것은 zip 파일입니다. 그것은 당신의 문제에 대한 답처럼 보이는

String name = fields.find(chooser.getLocation(), "mimic"); 
       Mimic mimic = getMimic(mimicList.get(0)); 
       String fileName = chooser.getSelectedFile().toString() + File.separator + "des_" +nombreMimic.substring(0, nombreMimic.length()-4)+ ".xml"; 
       String config = chooser.getSelectedFile().toString() + File.separator + "cfg_" +nombreMimic.substring(0, nombreMimic.length()-4)+".xml"; 
       FileOutputStream file; 
       FileOutputStream file2; 


       file = new FileOutputStream(fileName); 
       file2 =new FileOutputStream(config); 

       Parser parser; 
       parser = new Parser(file,new String[]{}); 
       parser.render(mimic , fields); 
       JOptionPane.showMessageDialog(null, "Complete!"); 

       Parser2 parser2; 
       parser2 = new Parser2(file2,new String[]{}); 
       parser2.render(mimic , fields); 
       JOptionPane.showMessageDialog(null, "Complete!"); 


       FileInputStream inputStream = new FileInputStream(chooser.getSelectedFile().toString() + File.separator + "des_" +nombreMimic.substring(0, nombreMimic.length()-4)+ ".xml"); 
       ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(nombreMimic.substring(0, nombreMimic.length()-4)+".des")); 

       zip.putNextEntry(new ZipEntry(fileName)); 

       byte [] buffer = new byte [1024]; 
       int bytesRead; 
       while((bytesRead=inputStream.read(buffer))>0){ 

       zip.write(buffer,0,bytesRead); 

       } 

       zip.closeEntry(); 
       zip.close(); 
       inputStream.close(); 


      } catch (Exception ex) { 
       Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 

       JOptionPane.showMessageDialog(null, "Error parsing"); 
      } 
     } 
    } 
}           
+0

Java 7을 사용하고 있습니까? – fge

+0

아니 아주 이전. 업데이트가 옵션이 아닙니다. 다른 항목에 통합 될 것입니다. –

답변

1

여기에 대답했다 : 자바를 사용하여 파일/폴더를 압축하는 방법에 대해 설명이 how to zip a folder itself using java

.

이 작동 할 폴더에 파일을 저장해야하지만,이 압축되면, 당신은 사용이 tutorial 다음 원래 폴더를 삭제할 수있는 유일한 저장

String path = "/path/to/file"; 
Files.delete(path); 

이 방법을 것입니다 zip 파일이어야합니다.

관련 문제