2015년 1월 27일 화요일

[Java] Unzip zip file.

Unzip zip file.

Source.

public class UnZip
{
    List<String> fileList;
    public UnZip(String zipFile, String outputFolder){

     byte[] buffer = new byte[1024];

     try{
     File folder = new File(outputFolder);
     if(!folder.exists()){
      folder.mkdir();
     }

     ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
     ZipEntry ze = zis.getNextEntry();

     while(ze!=null){

        String fileName = ze.getName();
           File newFile = new File(outputFolder + File.separator + fileName);

           MainFrame.str = ""+newFile.getAbsoluteFile();
           new File(newFile.getParent()).mkdirs();

           FileOutputStream fos = new FileOutputStream(newFile);            

           int len;
           while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
           }
           fos.close();  
           ze = zis.getNextEntry();
     }
        zis.closeEntry();
     zis.close();
   }   
}
explane
 
​public class UnZip //class
{
    List<String> fileList; //file that compressed.
    public UnZip(String zipFile, String outputFolder){ //Method

     byte[] buffer = new byte[1024]; //data

     try{ //Check forder and Zip File.
     File folder = new File(outputFolder);
     if(!folder.exists()){
      folder.mkdir();
     }

     ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); //read ZipFile.
     ZipEntry ze = zis.getNextEntry(); //new ZipEntry.

     while(ze!=null){ //Unzip

        String fileName = ze.getName(); //File name.
           File newFile = new File(outputFolder + File.separator + fileName); //File Dir

           new File(newFile.getParent()).mkdirs();

           FileOutputStream fos = new FileOutputStream(newFile); //File write.

           int len;
           while ((len = zis.read(buffer)) > 0) { //read File.
            fos.write(buffer, 0, len);
           }
           fos.close();  
           ze = zis.getNextEntry(); //Next File
     }
        zis.closeEntry(); //fin
     zis.close();
   }   
}


You can use this source like new UnZip("Zip file location","output Dir");

댓글 없음:

댓글 쓰기