`
flycun2
  • 浏览: 26831 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

运用JDK中 ZipInputStream类实现压缩文件的解压缩功能

    博客分类:
  • java
阅读更多
运用JDK中 ZipOutputStream类实现文件的压缩功能:http://flycun2.iteye.com/admin/blogs/1902657

将压缩文件log.zip 解压到output文件夹:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnZipExample {

	public static void main(String[] args) {
		String zipFile = "log.zip";
		String outputFolder = "output";
		byte[] buffer = new byte[1024];
		try {
			File folder = new File(outputFolder);
			if (!folder.exists()) {
				folder.mkdir();
			}
			//获取zip文件
			ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
			//获取zip文件里面的文件列表
			ZipEntry ze = zis.getNextEntry();
			while (ze != null) {
				String fileName = ze.getName();
				File newFile = new File(outputFolder + File.separator + fileName);
				System.out.println("文件解压 : " + newFile.getAbsoluteFile());
				//获取文件名中的路径创建文件夹
				new File(newFile.getParent()).mkdirs();
				FileOutputStream fos = new FileOutputStream(newFile);
				int len;
				while ((len = zis.read(buffer)) > 0) {
					fos.write(buffer, 0, len);
				}
				fos.close();
				ze = zis.getNextEntry();
			}
			zis.closeEntry();
			zis.close();
			System.out.println("End");
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}

0
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics