ZipFile&unZipFile

1515 ワード

	public static void zipFile() throws Exception {
		File file = new File("e:/ .zip");
		int leng = 0;
		byte[] b = new byte[1024];
		//  
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
		//  
		FileInputStream fis = new FileInputStream("d:/chen.txt");
		//  
		ZipEntry z1 = new ZipEntry("chen.txt");
		zos.putNextEntry(z1);

		while ((leng = fis.read(b)) != -1) {
			zos.write(b, 0, leng);
		}
		zos.close();
		fis.close();
	}

	/**
	 *  
	 * 
	 * @throws Exception
	 */
	public static void unZipFile() throws Exception {
		//  
		File file = new File("e:/ .zip");
		int leng = 0;
		byte[] b = new byte[1024];
		//  
		ZipFile zipFile = new ZipFile(file);
		//  
		String direc = "d:/";
		//  
		ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
		for (Enumeration enume = zipFile.entries(); enume.hasMoreElements();) {
			//  
			ZipEntry entry = (ZipEntry) enume.nextElement();
			File temFile = new File(direc + entry.getName());
			//  
			FileOutputStream fos = new FileOutputStream(temFile);
			while ((leng = zis.read(b)) != -1) {
				fos.write(b, 0, leng);
			}
			fos.close();
		}
		zis.close();
		System.out.println(zipFile.getName());
	}
}