Java复习之知识点整理(二十一)--- 压缩ZipOutputStream和解压缩ZipInputStream

一、压缩单个文件

//测试压缩单个文件
	@Test
	public void tsZipOneFile()
	{
		try {
			
			//创建压缩文件输出流
			OutputStream out = new FileOutputStream("E:\\A_学习资料\\JDK_API_1_6_zh_CN.CHM.ZIP");
			ZipOutputStream zout = new ZipOutputStream(out);
			
			//得到要压缩的字节数组
			InputStream in = new FileInputStream("E:\\A_学习资料\\JDK_API_1_6_zh_CN.CHM");
			int len = 0;
			byte [] buf = new byte[1024 * 1024 * 8];
			
			//指定Zip Entry(a.txt就是解压的时候显示的条目,可以是任意的字符串。但是一般与文件名一致)
			ZipEntry entry = new ZipEntry("a.txt");
			zout.putNextEntry(entry);
			
			//开始压缩
			while((len = in.read(buf)) != -1)
			{
				zout.write(buf,0,len);
			}
			
			//关闭流
			zout.close();
			in.close();
			out.close();
			
			System.out.println("压缩完成!");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


二、解压缩单个文件
	//测试解压缩单个文件		
	@Test
	public void tsUnZipOneFile()
	{
		try {
			
			//创建解压缩流
			InputStream in = new FileInputStream("E:\\A_学习资料\\JDK_API_1_6_zh_CN.CHM.ZIP");
			ZipInputStream zin = new ZipInputStream(in);
			//获取解压缩文件入口
			zin.getNextEntry();
	
			//创建解压缩文件输出流
			OutputStream out = new FileOutputStream("D:\\a.chm");
			
			//开始解压缩
			int len = 0;
			byte [] buf = new byte [1024];
			
			
			while( (len = zin.read(buf) ) != -1)
			{				
				out.write(buf,0,len);
			}
			out.close();
			zin.close();
			in.close();
					
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}


三、测试压缩多个文件

	@Test
	public void tsZipMultiFile()
	{
		try {
			
			//创建压缩文件输出流
			OutputStream out = new FileOutputStream("E:\\1.zip");
			ZipOutputStream zout = new ZipOutputStream(out);
			
			//得到要压缩的字节数组
			String [] srcFiles = new String[3];
			srcFiles[0] = "D:\\1.mp3";
			srcFiles[1] = "D:\\1.jpg";
			srcFiles[2] = "D:\\1.txt";
			
			for (String string : srcFiles) {
				
				InputStream in = new FileInputStream(string);
				int len = 0;
				byte [] buf = new byte[1024 * 8];
				
				//指定Zip Entry(a.txt就是解压的时候显示的条目,可以是任意的字符串。但是一般与文件名一致)
				File f = new File(string);
				ZipEntry entry = new ZipEntry(f.getName());
				zout.putNextEntry(entry);
				
				//开始压缩
				while((len = in.read(buf)) != -1)
				{
					zout.write(buf,0,len);
				}
				
				in.close();	
			}
			
			//关闭流
			zout.close();	
			out.close();
			
			System.out.println("压缩完成!");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


四、测试解压缩多个文件
//ZipInputStream,会在某个entry读完之后,返回-1;
	@Test
	public void tsUnZipMultiFile()
	{
		try {
			
			//获取待解压缩的文件
			InputStream in = new FileInputStream("E:\\1.zip");
			ZipInputStream zin = new ZipInputStream(in);
			//Zip入口
			ZipEntry entry;
			while( (entry = zin.getNextEntry()) != null)
			{				
				String name = entry.getName();	
				System.out.println("解压开始:" + name);
				OutputStream os = new FileOutputStream("E:\\" + name);
				
				byte [] buf = new byte[1024 * 8];
				int len = 0;
				while((len = zin.read(buf)) != -1)
				{					
					os.write(buf,0,len);
				}
				
				os.close();
			}
			zin.close();
			in.close();		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	

猜你喜欢

转载自blog.csdn.net/xcvbxv01/article/details/81053793