java 压缩文件与解压文件

该文章提供了一组Java代码,实现了对文件的GZIP压缩和解压缩,以及多个文件或文件夹的ZIP压缩和解压缩功能。代码包括了从输入流复制到输出流的方法,以及使用ZipOutputStream和ZipInputStream进行文件操作的相关方法。

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

直接上代码:

package com.cy.store.util;

import java.io.*;

import java.util.zip.*;

public class Jieysuo {

//复制输入流的内容到输出流

public static void copy(InputStream in,OutputStream out)throws IOException{

byte[] buf = new byte[4096];

int bytesRead = 0;

while ((bytesRead = in.read(buf))!=-1){

out.write(buf,0,bytesRead);

}

}

// 压缩一个文件

public static void gzip (String fileName) throws IOException {

InputStream in = null;

String gzipFileName = fileName + ".gz";

OutputStream out = null;

try {

in = new BufferedInputStream(new FileInputStream(fileName));

out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(gzipFileName)));

copy(in,out);

}finally {

if(out != null){

out.close();

}

if(in != null){

in.close();

}

}

}

// 解压缩一个文件

public static void gunzip (String gzipFileName,String unzipFileName) throws IOException {

InputStream in = null;

OutputStream out = null;

try {

in = new GZIPInputStream(new BufferedInputStream(new FileInputStream(gzipFileName)));

out = new BufferedOutputStream(new FileOutputStream(unzipFileName));

copy(in,out);

}finally {

if(out != null){

out.close();

}

if(in != null){

in.close();

}

}

}

// 多个文件或文件夹压缩

public static void zip(File inFile,File zipFile) throws IOException{

ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));

try {

if(!inFile.exists()){

throw new FileNotFoundException(inFile.getAbsolutePath());

}

inFile = inFile.getCanonicalFile();

String rootPath = inFile.getParent();

if(!rootPath.endsWith(File.separator)){

rootPath += File.separator;

}

addFileToZipOut(inFile,out,rootPath);

}finally {

out.close();

}

}

private static void addFileToZipOut(File file, ZipOutputStream out, String rootPath) throws IOException {

String relativePath = file.getCanonicalPath().substring(rootPath.length());

if(file.isFile()){

out.putNextEntry(new ZipEntry(relativePath));

InputStream in = new BufferedInputStream(new FileInputStream(file));

try {

copy(in,out);

}finally {

in.close();

}

}else {

out.putNextEntry(new ZipEntry(relativePath + File.separator));

for(File f: file.listFiles()){

addFileToZipOut(f,out,rootPath);

}

}

}

//解压文件

public static void unzip(File zipFile,String destDir)throws IOException{

ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));

if(!destDir.endsWith(File.separator)){

destDir += File.separator;

}

try {

ZipEntry entry = zin.getNextEntry();

while (entry != null){

extractZipEntry(entry,zin,destDir);

entry = zin.getNextEntry();

}

}finally {

zin.close();

}

}

private static void extractZipEntry(ZipEntry entry, ZipInputStream zin, String destDir) throws IOException {

if(!entry.isDirectory()){

File parent = new File(destDir + entry.getName()).getParentFile();

if(!parent.exists()){

parent.mkdirs();

}

OutputStream entryout = new BufferedOutputStream(new FileOutputStream(destDir + entry.getName()));

try {

copy(zin,entryout);

}finally {

entryout.close();

}

}else {

new File(destDir + entry.getName()).mkdirs();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值