1:下载压缩文件到本地服务器
//下载压缩文件
public static void main(String[] args) {
try {
// 初始化MinIO客户端
MinioClient minioClient = MinioClient.builder()
.endpoint("http://" + "192.168.16.188:9000")
.credentials("admin", "admin123456")
.build();
// 下载压缩文件到本地文件系统,在temp生成的临时文件
File zipFile = File.createTempFile("example", ".zip");
log.info("zipFile=====" + zipFile.getPath());
minioClient.downloadObject(DownloadObjectArgs.builder().bucket("otatest").object("ya2.zip").filename(zipFile.getAbsolutePath()).build());
}
2:先下载压缩文件,解压出里面的文件,下载到本地服务器指定位置,注意这种下载方式很慢,同时需要注意下载的文件夹目录是否有,不然那会报错
@SneakyThrows
public static void main(String[] args) {
try {
// 初始化MinIO客户端
MinioClient minioClient = MinioClient.builder()
.endpoint("http://" + "192.168.16.188:9000")
.credentials("admin", "admin123456")
.build();
// 下载压缩文件到本地文件系统
File zipFile = File.createTempFile("example", ".zip");
log.info("zipFile=====" + zipFile.getPath());
minioClient.downloadObject(DownloadObjectArgs.builder().bucket("otatest").object("模板2.zip").filename(zipFile.getAbsolutePath()).build());
// 解压文件
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile),Charset.forName("GBK"));
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
File entryFile = new File(zipEntry.getName());
File file = new File("C:\\Users\\dell\\Desktop\\22\\" + StringUtils.getFilename(entryFile.getName()));
org.springframework.util.FileCopyUtils.copy(entryFile, file);
zipEntry = zipInputStream.getNextEntry();
}
zipInputStream.closeEntry();
zipInputStream.close();
// 删除本地的压缩文件
zipFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
3:获取压缩文件流(这种方式比下载压缩文件快),解压出里面的文件,下载到本地服务器指定位置,注意这种下载方式很慢,同时需要注意下载的文件夹目录是否有,不然那会报错
@SneakyThrows
public static void main(String[] args) {
MinioClient minioClient = MinioClient.builder()
.endpoint("http://" + "192.168.16.188:9000")
.credentials("admin", "admin123456")
.build();
CustomMinioClient customMinioClient = new CustomMinioClient(minioClient);
ZipInputStream zis = null;
long s = System.currentTimeMillis();
// try (InputStream is = customMinioClient.getObject(GetObjectArgs.builder().bucket(getBucket()).object(objectName).build())) {
try (InputStream is = customMinioClient.getObject(GetObjectArgs.builder().bucket("otatest").object("模板.zip").build())) {
System.out.println(System.currentTimeMillis() - s);
// 创建ZipInputStream,用于读取ZIP文件
zis = new ZipInputStream(is, Charset.forName("UTF-8"));
// 遍历ZIP文件中的所有条目
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
// 如果当前条目是目录,则创建目录
if (entry.isDirectory()) {
new File(entry.getName()).mkdirs();
} else {
// 如果当前条目是文件,则解压缩该文件
FileOutputStream fos = new FileOutputStream(StringUtils.getFilename(entry.getName()));
byte[] buffer = new byte[1024];
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
zis.closeEntry();
//删除下载的文件
Files.delete(Paths.get(StringUtils.getFilename(entry.getName())));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭ZipInputStream
if (zis != null) {
zis.close();
}
}
}
4:获取压缩文件流(这种方式比下载压缩文件快),获取压缩文件里的每一个文件流,进行操作,上传到minio
@SneakyThrows
public static void main(String[] args) {
MinioClient minioClient = MinioClient.builder()
.endpoint("http://" + "192.168.16.188:9000")
.credentials("admin", "admin123456")
.build();
CustomMinioClient customMinioClient = new CustomMinioClient(minioClient);
ZipInputStream zis = null;
try (InputStream is = customMinioClient.getObject(GetObjectArgs.builder().bucket("otatest").object("模板.zip").build())) {
// 创建ZipInputStream,用于读取ZIP文件
zis = new ZipInputStream(is, Charset.forName("GBK"));
// 遍历ZIP文件中的所有条目
ZipEntry entry;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
while ((entry = zis.getNextEntry()) != null) {
// 如果当前条目是目录,则创建目录
if (entry.isDirectory()) {
new File(entry.getName()).mkdirs();
} else {
while (true) {
int len = zis.read(bytes);
if (len <= 0) {
break;
}
bos.write(bytes);
}
bos.flush();
bos.close();
zis.closeEntry();
InputStream bis = new ByteArrayInputStream(bos.toByteArray());
minioClient.putObject(PutObjectArgs.builder()
.bucket("otatest")
.object(entry.getName())
.stream(bis, -1L, 10485760L)
.build());
bis.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭ZipInputStream
if (zis != null) {
zis.close();
}
}
}
5:可以从压缩文件下载指定的文件
@SneakyThrows
public static void main13(String[] args) {
MinioClient minioClient = MinioClient.builder()
.endpoint("http://" + "192.168.16.188:9000")
.credentials("admin", "admin123456")
.build();
CustomMinioClient customMinioClient = new CustomMinioClient(minioClient);
ZipInputStream zipIn = null;
InputStream is = customMinioClient.getObject(GetObjectArgs.builder().bucket("otatest").object("模板.zip").build());
// 创建ZipInputStream,用于读取ZIP文件
zipIn = new ZipInputStream(is, Charset.forName("GBK"));
// 遍历ZIP文件中的所有条目
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
ZipEntry zipEntry;
//查询下载名字,可以是数组这样就可以下载多个文件,但是要修改逻辑代码
String[] files = {"java.txt"};
while ((zipEntry = zipIn.getNextEntry()) != null) {
for (String fileName:files) {
if (zipEntry.getName().equals(fileName)) {
while (true) {
int len = zipIn.read(bytes);
if (len <= 0) {
break;
}
bos.write(bytes);
}
bos.flush();
bos.close();
}
}
}
InputStream bis = new ByteArrayInputStream(bos.toByteArray());
bos.close();
zipIn.closeEntry();
zipIn.close();
bis.close();
}
6:获取压缩文件流(这种方式比下载压缩文件快),获取压缩文件里的每一个文件流,进行操作,上传到minio,这种方式推荐使用,最快
@SneakyThrows
public static void main(String[] args) {
MinioClient minioClient = MinioClient.builder()
.endpoint("http://" + "192.168.16.188:9000")
.credentials("admin", "admin123456")
.build();
CustomMinioClient customMinioClient = new CustomMinioClient(minioClient);
ZipInputStream zipIn = null;
try {
InputStream is = customMinioClient.getObject(GetObjectArgs.builder().bucket("otatest").object("模板.zip").build());
// 创建ZipInputStream,用于读取ZIP文件
zipIn = new ZipInputStream(is, Charset.forName("GBK"));
// 遍历ZIP文件中的所有条目
ZipEntry zipEntry;
//查询下载名字,可以是数组这样就可以下载多个文件,但是要修改逻辑代码
int i = 11;
while ((zipEntry = zipIn.getNextEntry()) != null) {
++i;
if (zipEntry.getSize() == 0) continue;
minioClient.putObject(PutObjectArgs.builder()
.bucket("otatest")
.object(i + "")
.stream(zipIn, -1L, 10485760L)
.build());
zipIn.closeEntry();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zipIn!=null) zipIn.closeEntry();
if (zipIn!=null) zipIn.close();
}
}
文章来源地址https://www.toymoban.com/news/detail-588679.html
文章来源:https://www.toymoban.com/news/detail-588679.html
到了这里,关于通过minio服务器压缩文件的下载和解压的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!