解压缩文件和压缩文件的方法

本文介绍了一个Java实现的GZIP文件压缩和解压缩工具类。该工具类提供了两个核心方法,分别用于将普通文件压缩为GZIP格式以及将GZIP格式文件解压缩为原始文件。代码详细展示了如何使用Java标准库中的GZIPInputStream和GZIPOutputStream进行文件操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.yqcf.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIP {
 /**
  *
  * 解压缩文件
  * @param inFileName:输入需要解压缩的文件的路径和文件名
  * @param outFileName:输出解压缩完的文件的路径和文件名
  * @return boolean
  */
 public boolean uncoilGZIP(String inFileName, String outFileName) {
  // ***************判断参数的是否合理*******************//
  if (inFileName == null || "".equals(inFileName) || outFileName == null
    || "".equals(outFileName)) {
   System.out.println("解压缩文件失败:文件名为空");
   return false;
  }
  File inFile = new File(inFileName);
  File outFile = new File(outFileName);
  if (!inFile.exists()) {
   System.out.println("解压缩文件失败:待解压缩文件名或路径不存在");
   return false;
  }
  GZIPInputStream input = null;
  OutputStream output = null;
  try {
   if (!outFile.exists()) {
    if (!outFile.createNewFile()) {
     System.out.println("解压缩文件失败:解压缩后的文件的路径不存在");
     return false;
    }
   }
   // 建立gzip压缩文件输入流
   input = new GZIPInputStream(new FileInputStream(inFile));
   // 建立解压文件输出流
   output = new FileOutputStream(outFile);
   // 设定读入缓冲区尺寸
   byte[] block = new byte[1024];
   int len = 0;
   while ((len = input.read(block)) != -1) {
    output.write(block, 0, len);
   }
   output.flush();
   return true;
  } catch (IOException e) {
   System.out.println("解压缩文件失败:" + e.getMessage());
   return false;
  } finally {
   // !!!关闭流,保证输入输出完整和释放系统资源.
   try {
    if (input != null) {
     input.close();
    }
    if (output != null) {
     output.close();
    }
   } catch (IOException ex) {
    System.out.println(ex.getMessage());
   }
  }
 }

 /**
  *
  * 压缩文件
  * @param inFileName:输入要压缩的文件的路径和文件名
  * @param outFileName:输出已压缩完的文件的路径和文件名
  * @return boolean
  */
 public boolean compressGZIP(String inFileName, String outFileName) {
  // ***************判断参数的是否合理*******************//
  if (inFileName == null || "".equals(inFileName) || outFileName == null
    || "".equals(outFileName)) {
   System.out.println("压缩文件失败:文件名为空");
   return false;
  }
  File inFile = new File(inFileName);
  File outFile = new File(outFileName);
  if (!inFile.exists()) {
   System.out.println("压缩文件失败:待压缩文件名或路径不存在");
   return false;
  }
  GZIPOutputStream output = null;
  InputStream input = null;
  try {
   if (!outFile.exists()) {
    if (!outFile.createNewFile()) {
     System.out.println("压缩文件失败:压缩后文件的路径不存在");
     return false;
    }
   }
   // 打开需压缩文件作为文件输入流
   input = new FileInputStream(inFile);
   // 建立gzip压缩输出流
   output = new GZIPOutputStream(new FileOutputStream(outFile));
   // 设定读入缓冲区尺寸
   byte[] block = new byte[1024];
   int len = 0;
   while ((len = input.read(block)) != -1) {
    output.write(block, 0, len);
   }
   output.flush();
   return true;
  } catch (IOException e) {
   System.out.println("压缩文件失败:" + e.getMessage());
   return false;
  } finally {
   // !!!关闭流,保证输入输出完整和释放系统资源.
   try {
    if (input != null) {
     input.close();
    }
    if (output != null) {
     output.close();
    }
   } catch (IOException ex) {
    System.out.println(ex.getMessage());
   }
  }
 }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值