2008년 10월 31일 금요일

java zip 압축관련.


import java.io.*;
import java.util.zip.*;

 

public void creatZipFile() throws Exception {
  

    File f = new File("D:/file/fileup");
  
    String path = "D:/file/fileup";

    String files[] = f.list(); // f object 에 있는 파일목록
    
    // Create a buffer for reading the files
    byte[] buf = new byte[1024];
   
    try {

   

        // Create the ZIP file
        String outFilename = "D:/outfile.zip";
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
   
        // Compress the files
        for (int i=0; i<files.length; i++) {
    
            FileInputStream in = new FileInputStream( path + "/" + files[i]);
   
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(files[i])); // Zip 파일에 경로를 정하여 저장할수 있다.
    
            // Transfer bytes from the file to the ZIP file
            int len;
            while ((len = in.read(buf)) > 0) {

                out.write(buf, 0, len);
            }
   
            // Complete the entry
            out.closeEntry();
            in.close();
        }
   
        // Complete the ZIP file
        out.close();
    }catch(Exception e) {

        throw e;
    }

}

 

방법2. (BufferedOutputStream, BufferedInputStream 사용)

import java.io.*;
import java.util.zip.*;

 

public void creatZipFile() throws Exception {
  

    File f = new File("D:/file/fileup");

    int size = 1024;
    String path = "D:/file/fileup";

    String files[] = f.list(); // f object 에 있는 파일목록

    // Create a buffer for reading the files
    byte[] buf = new byte[size];
    try {

 

        // Create the ZIP file
        String outFilename = "D:/outfile.zip";
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFilename)));

        // Compress the files
        for (int i=0; i<files.length; i++) {
    
            FileInputStream fs = new FileInputStream( path + "/" + files[i]);

            BufferedInputStream in = new BufferedInputStream(fs, size);


            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(files[i])); // Zip 파일에 경로를 정하여 저장할수 있다.
    
            // Transfer bytes from the file to the ZIP file
            int len;
            while ((len = in.read(buf, 0, size)) > 0) {

                out.write(buf, 0, len);
            }
   
            // Complete the entry
            out.closeEntry();
            in.close();
        }
   
        // Complete the ZIP file
        out.close();
    }catch(Exception e) {

        throw e;
    }

}

댓글 없음:

댓글 쓰기