🚀 feat(新增): ZIP文件
This commit is contained in:
parent
f38bffe06a
commit
9d2894b141
|
@ -1,4 +1,33 @@
|
|||
package cn.bunny;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class FileTest {
|
||||
@SneakyThrows
|
||||
public static void main(String[] args) {
|
||||
String zipFilePath = "D:\\MyFolder\\文档和软件\\图片.zip";
|
||||
try (FileInputStream fis = new FileInputStream(zipFilePath);
|
||||
ZipInputStream zis = new ZipInputStream(fis)) {
|
||||
|
||||
ZipEntry entry = zis.getNextEntry();
|
||||
while (entry != null) {
|
||||
System.out.println("正在读取: " + entry.getName());
|
||||
|
||||
// 这里可以添加代码来读取entry的内容
|
||||
// 例如,如果entry是一个文件,你可以使用BufferedReader来读取文本文件的内容
|
||||
|
||||
// 读取下一个条目
|
||||
entry = zis.getNextEntry();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// File file = new File("D:\\MyFolder\\文档和软件\\图片.zip");
|
||||
// ZipFile zipFile = new ZipFile(file);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package cn.bunny;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class ZipFIleTest {
|
||||
public static void main(String[] args) {
|
||||
String zipFilePath = "D:\\MyFolder\\文档和软件\\output.zip";
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
|
||||
// 添加条目到ZIP文件
|
||||
ZipEntry entry = new ZipEntry("example.txt");
|
||||
zos.putNextEntry(entry);
|
||||
|
||||
// 写入数据到条目
|
||||
String data = "Hello, World!";
|
||||
zos.write(data.getBytes());
|
||||
zos.closeEntry();
|
||||
|
||||
// 可以继续添加更多条目
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package cn.bunny;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class ZipOutFIleTest {
|
||||
public static void main(String[] args) {
|
||||
String zipFilePath = "D:\\MyFolder\\图片.zip";
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
|
||||
// 添加条目到ZIP文件
|
||||
ZipEntry entry = new ZipEntry("example.txt");
|
||||
zos.putNextEntry(entry);
|
||||
|
||||
// 写入数据到条目
|
||||
String data = "Hello, World!";
|
||||
zos.write(data.getBytes());
|
||||
zos.closeEntry();
|
||||
|
||||
// 可以继续添加更多条目
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue