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");

[Java] Play wav file.

wav file play source.

public class Sound {
 public Sound(String file, boolean Loop){
  Clip clip;
  try {
  AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file)));
  clip = AudioSystem.getClip();
  clip.open(ais);
  clip.start();
  if ( Loop) clip.loop(-1);
  }
  catch (Exception e) {
   e.printStackTrace();
  }
 }
}

explane.

public class Sound { // class
 public Sound(String file, boolean Loop){ // method
  Clip clip; //new Clip
  try {
  AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file))); //read wav file.
  clip = AudioSystem.getClip(); //setting clip.
  clip.open(ais); //read ais in clip
  clip.start(); //Music start.
  if ( Loop) clip.loop(-1); //if Loof = true, play over and over.
  }
  catch (Exception e) { //exception
   e.printStackTrace();
  }
 }
}

in main method write new Sound("File Dir",false);

[Java] File download.(BufferInputStream)

Java File Download source.



inputstream과 outputstream을 사용.
try {
         URL url=new URL("URL");
         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
         int filesize = connection.getContentLength();
         float totalDataRead=0;
         java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
         java.io.FileOutputStream fos = new java.io.FileOutputStream("C:/temp.zip");
         java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
         byte[] data = new byte[1024];
         int i=0;
         while((i=in.read(data,0,1024))>=0) {
               totalDataRead=totalDataRead+i;
               bout.write(data,0,i);
               float Percent=(totalDataRead*100)/filesize;
         }
         bout.close();
         in.close();
}
catch(Exception e1) { }

explane
try {
         URL url=new URL("URL");  //File download adress
         HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //connect URL
         int filesize = connection.getContentLength(); //파일 사이즈를 가져옵니다.
         float totalDataRead=0; // totalData read (byte)
         BufferedInputStream in = newBufferedInputStream(connection.getInputStream()); //read File from url
         OutputStream fos = new FileOutputStream("C:/temp.zip"); //save adress.
         BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
         byte[] data = new byte[1024];
         int i=0;
         while((i=in.read(data,0,1024))>=0) { //Start Downloading
               totalDataRead=totalDataRead+i;
               bout.write(data,0,i); //download
               float Percent=(totalDataRead*100)/filesize; //Percent of Download.
               System.out.println(Percent);
         }
         bout.close();
         in.close();
}
catch(Exception e1) { }
ㅃㅃ.