收集总结java中压缩、解压文件方法

1.使用GZIPOutputStream压缩文件:

	@org.junit.Test
	public void test013 (){
		String  str = "F:\\MyTest\\myTestFile.txt";
		//开始压缩文件到一个.gz包中  而这里的.gz之前的文件名可与之前不一致,如下面的文件名,
		//使用解压工具看到里面的文件是去掉.gz之前的部分myTestFile001.txt
		try (FileOutputStream fileOutputStream = new FileOutputStream("F:\\MyTest\\myTestFile001.txt.gz");
				GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream)) {
				FileInputStream fileInputStream  = new FileInputStream(str);
				byte[] b = new byte[1024*1024*5];
				int length = 0;
				while ((length = fileInputStream.read(b)) != -1) {
					gzipOutputStream.write(b, 0, length);
				}
				fileInputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			} 
	}
2. 使用 GZIPOutputStream 解压指定文件:
@org.junit.Test
	public void test008(){
        String ouputfile = "";
        String sourcedir = "F:\\MyTest\\myTestFile001.txt.gz";
        
        System.out.println(sourcedir.substring(0,sourcedir.lastIndexOf('.')));
        try(  //建立gzip压缩文件输入流 
                FileInputStream fin = new FileInputStream(sourcedir);   
                //建立gzip解压工作流
                GZIPInputStream gzin = new GZIPInputStream(fin)){  
        	
            //建立解压文件输出流文件路劲 
            ouputfile = sourcedir.substring(0,sourcedir.lastIndexOf('.'));
            
            FileOutputStream fout = new FileOutputStream(ouputfile);   
            
            int num;
            byte[] buf = new byte[1024*1024*5];
            while ((num = gzin.read(buf,0,buf.length)) != -1)
            {   
                fout.write(buf,0,num);   
            }
            fout.close();
        } catch (Exception ex){  
        	ex.printStackTrace();
        }  
    }
3. 使用 ZipOutputStream压缩多个 指定文件:

@org.junit.Test
	public void test014 (){
		String [] strArrays = {"F:\\MyTest\\myTestFile001.txt","F:\\MyTest\\myTestFile001.txt.gz"};
		try(ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream("F:\\MyTest\\myTest.zip"));) {
			for (String string : strArrays) {
				File file = new File(string);
				//和TarOutputStream的TarEntry放的是File类型,这里放的是文件名
				ZipEntry zipEntry = new ZipEntry(file.getName());
				zipOutputStream.putNextEntry(zipEntry);
				FileInputStream fileInputStream  = new FileInputStream(string);
				byte[] b = new byte[1024*1024*5];
				int length = 0;
				while ((length = fileInputStream.read(b)) != -1) {
					zipOutputStream.write(b, 0, length);
				}
				
				fileInputStream.close();
				zipOutputStream.closeEntry();
				//调用了这个方法之后,后面的文件是不能被添加的,压缩包里面只有第一个文件,
//				gzipOutputStream.finish();
			}
			} catch (Exception e) {
				e.printStackTrace();
			} 
	}
4.解压zip包:

@org.junit.Test
	public void test015 (){
		String gzFileStr = "F:\\MyTest\\myTest.zip";
		File file = new File(gzFileStr);
		try (ZipFile zf = new ZipFile(gzFileStr);){
			Enumeration<? extends ZipEntry> entries = zf.entries();
				
			while (entries.hasMoreElements()) {
				ZipEntry entry = entries.nextElement();
				String zipEntryName = entry.getName();
				
				System.out.println(zipEntryName);
				FileOutputStream fileOutputStream = new FileOutputStream(file.getParent()+"\\"+"unzip"+zipEntryName);
				InputStream inputStream = zf.getInputStream(entry);
				byte[] b = new byte[1024*1024*5];
				int length = 0;
				while ((length = inputStream.read(b)) != -1) {
					fileOutputStream.write(b, 0, length);
				}
				fileOutputStream.close();
				inputStream.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

5.使用TarOutputStream打包多个文件:(只是打包,没有压缩文件):

	@org.junit.Test
	public void test011 (){
		String [] strArrays = {"F:\\MyTest\\myTestFile001.txt","F:\\MyTest\\myTestFile001.txt.gz"};
		try (  
				FileOutputStream fileOutputStream = new FileOutputStream("F:\\MyTest\\myTest.tar");
				TarOutputStream tarOutputStream = new TarOutputStream(fileOutputStream);
				//先放到内存中
//				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//				TarOutputStream tarOutputStream = new TarOutputStream(byteArrayOutputStream);
				){
			
			for (String string : strArrays) {
				File file = new File(string);
				TarEntry tarEntry = new TarEntry(file);
				tarEntry.setName(file.getName()); 
				tarOutputStream.putNextEntry(tarEntry);
				FileInputStream fileInputStream  = new FileInputStream(string);
				byte[] b = new byte[1024*1024*5];
				int length = 0;
				while ((length = fileInputStream.read(b)) != -1) {
					tarOutputStream.write(b, 0, length);
				}
				
				fileInputStream.close();
				tarOutputStream.flush();
				tarOutputStream.closeEntry(); 
			}
			tarOutputStream.flush();
			tarOutputStream.finish();
			
			//如果是在内存中的数据,则使用工具类写入到文件
//			byte[] byteArray = byteArrayOutputStream.toByteArray();
//			IOUtils.write(byteArray, fileOutputStream);
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
6.解压TarOutputStream打包的文件:

@org.junit.Test
	public void test012 (){
		String gzFileStr = "F:\\MyTest\\myTest.tar";//使用tar打包的,使用zip解压错误
		File file = new File(gzFileStr);
		try {
			TarInputStream  tarTestFile = new TarInputStream(new FileInputStream(file));  
			TarEntry nextEntry = null;
			while ((nextEntry = tarTestFile.getNextEntry())!= null) {
				String tarEntryName = nextEntry.getName();//获取压缩包中 的文件名
				System.out.println(tarEntryName);
				FileOutputStream fileOutputStream = new FileOutputStream(file.getParent()+"\\"+"untar"+tarEntryName);
				byte[] b = new byte[1024*1024*5];
				int length = 0;
				while ((length = tarTestFile.read(b)) != -1) {
					fileOutputStream.write(b, 0, length);
				}
				fileOutputStream.close();
			}
			tarTestFile.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

7.ZipOutputStream压缩一个目录 不改变目录的结构

/**
	 * 压缩一个目录 不改变目录的结构
	 * @Function:  Test.java
	 * @Description: 
	 * 
	 * @return void
	 * @version: v1.0.0
	 * @date:  2017年11月19日 下午4:33:06
	 */
	@org.junit.Test
	public void test021 (){
		String  strFolder = "D:\\tmp";
		File fileFolder = new File(strFolder);
		File[] listFiles = fileFolder.listFiles();
		try(ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream("D:\\tmp.zip"))) {
			for (File file : listFiles) {
				getZipOutPutStreamEntryFile(zipOutputStream,file);
			}
			} catch (Exception e) {
				e.printStackTrace();
			} 
	}




	private void getZipOutPutStreamEntryFile(ZipOutputStream zipOutputStream, File file) throws Exception {
		
		if (file.isDirectory()) {
			File[] listFiles = file.listFiles();
			for (File file2 : listFiles) {
				getZipOutPutStreamEntryFile(zipOutputStream, file2);
			}
			
		} else {
			try {
				String path = file.getPath();
				//和TarOutputStream的TarEntry放的是File类型,这里放的是文件名
				ZipEntry zipEntry = new ZipEntry(path.substring(path.indexOf("\\")+1, path.length()));
				zipOutputStream.putNextEntry(zipEntry);
				FileInputStream fileInputStream  = new FileInputStream(file.getPath());
				byte[] b = new byte[1024*1024*5];
				int length = 0;
				while ((length = fileInputStream.read(b)) != -1) {
					zipOutputStream.write(b, 0, length);
				}
				
				fileInputStream.close();
				zipOutputStream.closeEntry();
				//调用了这个方法之后,后面的文件是不能被添加的,压缩包里面只有第一个文件,
//			gzipOutputStream.finish();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值