您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在Java中,可以使用java.util.zip
包中的类来处理ZIP文件
如果你使用Maven,可以在pom.xml
文件中添加以下依赖:
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
</dependencies>
import org.apache.commons.compress.archivers.zip.*;
import java.io.*;
import java.nio.file.*;
import java.util.zip.ZipEntry;
public class UnzipUtility {
public static void main(String[] args) {
String zipFilePath = "path/to/your/zipfile.zip";
String destDirectory = "path/to/your/destination/directory";
try {
unzip(zipFilePath, destDirectory);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zipIn, filePath);
} else {
File dir = new File(filePath);
dir.mkdirs();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
byte[] bytesIn = new byte[4096];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
}
}
}
import org.apache.commons.compress.archivers.zip.*;
import java.io.*;
import java.nio.file.*;
public class ZipUtility {
public static void main(String[] args) {
String sourceDirectory = "path/to/your/source/directory";
String zipFilePath = "path/to/your/zipfile.zip";
try {
createZip(sourceDirectory, zipFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createZip(String sourceDirectory, String zipFilePath) throws IOException {
File sourceDir = new File(sourceDirectory);
if (!sourceDir.isDirectory()) {
throw new IllegalArgumentException("Source directory is not a valid directory");
}
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
addDirectoryToZip(sourceDir, sourceDir.getName(), zipOut);
}
}
private static void addDirectoryToZip(File directory, String baseName, ZipOutputStream zipOut) throws IOException {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
addFileToZip(file, baseName + File.separator + file.getName(), zipOut);
}
}
}
private static void addFileToZip(File file, String fileName, ZipOutputStream zipOut) throws IOException {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
byte[] bytesIn = new byte[4096];
int read = 0;
while ((read = bis.read(bytesIn)) != -1) {
zipOut.write(bytesIn, 0, read);
}
}
}
}
这些示例展示了如何读取ZIP文件中的内容并将其提取到目标目录,以及如何将多个文件和目录打包到一个ZIP文件中。你可以根据需要修改这些示例以满足你的需求。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。