'자바 zip'에 해당되는 글 1건

부록 : zip 압축 풀기.

 

[Java] 자바 zip 압축 풀기 ( 파일 , 폴더 압축 풀기)

# 자바로 zip 파일 압축 풀기. - 폴더 안에 파일과, 하위 폴더 압축해버리기. - 아래의 ZIP 파일 압축 풀기. # UnZip.java - ZIP 파일 압축 풀기 package zipUtil; import java.io.File; import java.io.FileInp..

minaminaworld.tistory.com


# 자바로 파일 , 폴더 zip로 압축하기 

 

    - 폴더 안에 파일과, 하위 폴더 압축해버리기. 

 

    - 아래의 폴더의 위치를 압축하겠습니다.

 


# CompressZip.java - ZIP 파일을 만들 유틸 

package zipUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CompressZip {
	/**
	 * @description 압축 메소드 
	 * @param path 압축할 폴더 경로
	 * @param outputFileName 출력파일명
	 */
	public boolean compress(String path, String outputPath, String outputFileName) throws Throwable {
		// 파일 압축 성공 여부 
		boolean isChk = false;
		
		File file = new File(path);
		
		// 파일의 .zip이 없는 경우, .zip 을 붙여준다. 
		int pos = outputFileName.lastIndexOf(".") == -1 ? outputFileName.length() : outputFileName.lastIndexOf(".");
		
		// outputFileName .zip이 없는 경우 
		if (!outputFileName.substring(pos).equalsIgnoreCase(".zip")) {
			outputFileName += ".zip";
		}
		
		// 압축 경로 체크
		if (!file.exists()) {
			throw new Exception("Not File!");
		}
		
		// 출력 스트림
		FileOutputStream fos = null;
		// 압축 스트림
		ZipOutputStream zos = null;
		
		try {
			fos = new FileOutputStream(new File(outputPath + outputFileName));
			zos = new ZipOutputStream(fos);
			
			// 디렉토리 검색를 통한 하위 파일과 폴더 검색 
			searchDirectory(file, file.getPath(), zos);
			
			// 압축 성공.
			isChk = true;
		} catch (Throwable e) {
			throw e;
		} finally {
			if (zos != null)
				zos.close();
			if (fos != null)
				fos.close();
		}
		return isChk;
	}

	/**
	 * @description 디렉토리 탐색
	 * @param file 현재 파일
	 * @param root 루트 경로
	 * @param zos  압축 스트림
	 */
	private void searchDirectory(File file, String root, ZipOutputStream zos) throws Exception {
		// 지정된 파일이 디렉토리인지 파일인지 검색
		if (file.isDirectory()) {
			// 디렉토리일 경우 재탐색(재귀)
			File[] files = file.listFiles();
			for (File f : files) {
				System.out.println("file = > " + f);
				searchDirectory(f, root, zos);
			}
		} else {
			// 파일일 경우 압축을 한다.
			try {
				compressZip(file, root, zos);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @description압축 메소드 
	 * @param file
	 * @param root
	 * @param zos
	 * @throws Throwable
	 */
	private void compressZip(File file, String root, ZipOutputStream zos) throws Throwable {
		FileInputStream fis = null;
		try {
			String zipName = file.getPath().replace(root + "\\", "");
			// 파일을 읽어드림
			fis = new FileInputStream(file);
			// Zip엔트리 생성(한글 깨짐 버그)
			ZipEntry zipentry = new ZipEntry(zipName);
			// 스트림에 밀어넣기(자동 오픈)
			zos.putNextEntry(zipentry);
			int length = (int) file.length();
			byte[] buffer = new byte[length];
			// 스트림 읽어드리기
			fis.read(buffer, 0, length);
			// 스트림 작성
			zos.write(buffer, 0, length);
			// 스트림 닫기
			zos.closeEntry();

		} catch (Throwable e) {
			throw e;
		} finally {
			if (fis != null)
				fis.close();
		}
	}

}

 

 

 


# Main.java - ZIP 파일을 만들 유틸 

 

 

package Main;

import zipUtil.CompressZip;
//import zipUtil.UnZip;

/**
 * Main
 */
public class Main {
	public static void main(String[] args) {

		// 압축 파일 위치와 압축된 파일
		// String zipPath = "G:/ZIP_TEST/";
		// String zipFile = "jsmpeg-player-master.zip";

		// 압축을 해제할 위치, 압축할 파일이름
		String unZipPath = "G:/ZIP_TEST/TEST/";
		String unZipFile = "jsmpeg-player";

//		System.out.println("--------- 압축 해제 ---------");
//		UnZip unZip = new UnZip();
//		// 압축 해제 
//		if (!unZip.unZip(zipPath, zipFile, unZipPath)) {
//			System.out.println("압축 해제 실패");
//		}

		System.out.println("--------- 압축 하기 ---------");
		CompressZip compressZip = new CompressZip();

		// 압축 하기
		try {
			if (!compressZip.compress("G:/ZIP_TEST/TEST/jsmpeg-player-master", unZipPath, unZipFile)) {
				System.out.println("압축 실패");
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}

# 결과화면

블로그 이미지

미나미나미

,