압축하기 [D 의 1234 폴더에 a 와 b 텍스트 파일 필요.]

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

   

public class text1 {

 /**

  * @param args

  */

 public static void main(String[] args) {

  // TODO Auto-generated method stub

  // 압축할 파일에 포함시킬 파일들

      String[] files = new String[]{"D:\\1234\\a.txt", "D:\\1234\\b.txt"};

        

      // 파일을 읽기위한 버퍼

      byte[] buf = new byte[1024];

        

      try {

          // 압축파일명

          String zipName = "D:\\1234\\ab.zip";

          ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipName));

        

          // 파일 압축

          for (int i=0; i<files.length; i++) {

              FileInputStream in = new FileInputStream(files[i]);

        

              // 압축 항목추가

              out.putNextEntry(new ZipEntry(files[i]));

        

              // 바이트 전송

              int len;

              while ((len = in.read(buf)) > 0) {

                  out.write(buf, 0, len);

              }

        

              out.closeEntry();

              in.close();

          }

        

          // 압축파일 작성

          out.close();

      } catch (IOException e) {

       e.printStackTrace();

      }

 }

}

   

압축 풀기 [D의 1234 폴더에 ab파일.]

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

public class unzip{

 public static void main(String[] args) {

  try {

   // 압축파일 열기

   String zipfile = "D:\\1234\\ab.zip";

   ZipInputStream in = new ZipInputStream(new FileInputStream(

     zipfile));

   // 엔트리 취득

   ZipEntry entry = null;

   while ((entry = in.getNextEntry()) != null) {

    System.out.println(entry.getName() + "에 압축을 풉니다.");

    OutputStream out = new FileOutputStream(entry.getName());

    // 버퍼

    byte[] buf = new byte[1024];

    int len;

    while ((len = in.read(buf)) > 0) {

     out.write(buf, 0, len);

    }

      

    in.closeEntry();

    // out닫기

    out.close();

   }

   in.close();

  } catch (IOException e) {

   e.printStackTrace();

  }

 }

}

   

   


WRITTEN BY
미냐브
게임,유머,게임제작 4보단 3

,