'unzip'에 해당되는 글 1건

  1. 2008.11.05 JAR 파일 풀기
posted by 희정냥★ 2008. 11. 5. 01:25
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class UnZip {

	/** The ZipFile that is used to read an archive */
	protected ZipFile zippy;

	/** The buffer for reading/writing the ZipFile data */
	protected byte[] b;

	/**
	 * Simple main program, construct an UnZipper, process each .ZIP file from
	 * argv[] through that object.
	 */
	public static void main(String[] argv) {
		UnZip u = new UnZip();

		String filename = "c:/cutewebi.jar";
		u.unZip(filename);

		System.err.println("All done!");
	}

	/** Construct an UnZip object. Just allocate the buffer */
	UnZip() {
		b = new byte[8092];
	}

	/** Cache of paths we've mkdir()ed. */
	protected SortedSet dirsMade;

	/** For a given Zip file, process each entry. */
	public void unZip(String fileName) {
		dirsMade = new TreeSet();
		try {
			zippy = new ZipFile(fileName);
			Enumeration all = zippy.entries();
			while (all.hasMoreElements()) {
				getFile((ZipEntry) all.nextElement());
			}
		} catch (IOException err) {
			System.err.println("IO Error: " + err);
			return;
		}
	}

	protected boolean warnedMkDir = false;

	/**
	 * Process one file from the zip, given its name. Either print the name, or
	 * create the file on disk.
	 */
	protected void getFile(ZipEntry e) throws IOException {
		String zipName = ("C:/" + e.getName());

		if (zipName.startsWith("/")) {
			if (!warnedMkDir)
				System.out.println("Ignoring absolute paths");
			warnedMkDir = true;
			zipName = zipName.substring(1);
		}
		// if a directory, just return. We mkdir for every file,
		// since some widely-used Zip creators don't put out
		// any directory entries, or put them in the wrong place.
		if (zipName.endsWith("/")) {
			return;
		}
		// Else must be a file; open the file for output
		// Get the directory part.
		int ix = zipName.lastIndexOf('/');
		if (ix > 0) {
			String dirName = zipName.substring(0, ix);
			if (!dirsMade.contains(dirName)) {
				File d = new File(dirName);
				// If it already exists as a dir, don't do anything
				if (!(d.exists() && d.isDirectory())) {
					// Try to create the directory, warn if it fails
					System.out.println("Creating Directory: " + dirName);
					if (!d.mkdirs()) {
						System.err.println("Warning: unable to mkdir "
								+ dirName);
					}
					dirsMade.add(dirName);
				}
			}
		}

		System.err.println("Creating " + zipName);
		FileOutputStream os = new FileOutputStream(zipName);
		InputStream is = zippy.getInputStream(e);
		int n = 0;
		while ((n = is.read(b)) > 0)
			os.write(b, 0, n);
		is.close();
		os.close();

	}
}

'Computer > Programming' 카테고리의 다른 글

Java Project에서 FormToolkit 런타임 에러.  (1) 2008.11.10
Canvas(SWT)에 보이는 화면을 이미지 파일로 저장하기  (0) 2008.11.05
JAR 파일 풀기  (0) 2008.11.05
Java 소스 컴파일하기  (0) 2008.11.04
jar 파일 목록 보기  (0) 2008.11.04
Jar 파일 읽어오기  (0) 2008.10.30
TAG

댓글을 달아 주세요