java 解压压缩文件
时间: 2023-12-31 10:25:02 浏览: 84
java 解压缩文件
4星 · 用户满意度95%
以下是使用Java进行文件压缩和解压缩的示例代码:
1. 使用java.util.zip包进行压缩和解压缩:
```java
import java.io.*;
import java.util.zip.*;
public class ZipUtil {
public static void compressFile(String sourceFilePath, String destinationFilePath) throws IOException {
FileOutputStream fos = new FileOutputStream(destinationFilePath);
ZipOutputStream zipOut = new ZipOutputStream(fos);
File fileToZip = new File(sourceFilePath);
zipFile(fileToZip, fileToZip.getName(), zipOut);
zipOut.close();
fos.close();
}
private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
if (fileToZip.isHidden()) {
return;
}
if (fileToZip.isDirectory()) {
if (fileName.endsWith("/")) {
zipOut.putNextEntry(new ZipEntry(fileName));
zipOut.closeEntry();
} else {
zipOut.putNextEntry(new ZipEntry(fileName + "/"));
zipOut.closeEntry();
}
File[] children = fileToZip.listFiles();
for (File childFile : children) {
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
}
return;
}
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
fis.close();
}
public static void decompressFile(String zipFilePath, String destinationFolderPath) throws IOException {
File destDir = new File(destinationFolderPath);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = destinationFolderPath + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zipIn, filePath);
} else {
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[1024];
int read;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
}
```
2. 使用Apache Commons Compress库进行压缩和解压缩:
```java
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
public class ZipUtil {
public static void compressFile(String sourceFilePath, String destinationFilePath) throws IOException {
FileOutputStream fos = new FileOutputStream(destinationFilePath);
ArchiveOutputStream zipOut = new ZipArchiveOutputStream(fos);
File fileToZip = new File(sourceFilePath);
zipFile(fileToZip, fileToZip.getName(), zipOut);
zipOut.close();
fos.close();
}
private static void zipFile(File fileToZip, String fileName, ArchiveOutputStream zipOut) throws IOException {
if (fileToZip.isHidden()) {
return;
}
if (fileToZip.isDirectory()) {
if (fileName.endsWith("/")) {
zipOut.putArchiveEntry(new ZipArchiveEntry(fileName));
zipOut.closeArchiveEntry();
} else {
zipOut.putArchiveEntry(new ZipArchiveEntry(fileName + "/"));
zipOut.closeArchiveEntry();
}
File[] children = fileToZip.listFiles();
for (File childFile : children) {
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
}
return;
}
FileInputStream fis = new FileInputStream(fileToZip);
ArchiveEntry zipEntry = new ZipArchiveEntry(fileName);
zipOut.putArchiveEntry(zipEntry);
IOUtils.copy(fis, zipOut);
fis.close();
zipOut.closeArchiveEntry();
}
public static void decompressFile(String zipFilePath, String destinationFolderPath) throws IOException {
File destDir = new File(destinationFolderPath);
if (!destDir.exists()) {
destDir.mkdir();
}
FileInputStream fis = new FileInputStream(zipFilePath);
ArchiveInputStream zipIn = new ZipArchiveInputStream(fis);
ArchiveEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = destinationFolderPath + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zipIn, filePath);
} else {
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
fis.close();
}
private static void extractFile(ArchiveInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
IOUtils.copy(zipIn, bos);
bos.close();
}
}
```
阅读全文