1: Download the compressed file to the local server
//Download the compressed file
public static void main(String[] args) {<!-- -->
try {<!-- -->
// Initialize the MinIO client
MinioClient minioClient = MinioClient. builder()
.endpoint("http://" + "192.168.16.188:9000")
.credentials("admin", "admin123456")
.build();
// Download the compressed file to the local file system, a temporary file generated in 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: Download the compressed file first, unzip the file inside, and download it to the specified location on the local server. Note that this download method is very slow. At the same time, you need to pay attention to whether the downloaded folder exists, or it will report an error
@SneakyThrows
public static void main(String[] args) {<!-- -->
try {<!-- -->
// Initialize the MinIO client
MinioClient minioClient = MinioClient. builder()
.endpoint("http://" + "192.168.16.188:9000")
.credentials("admin", "admin123456")
.build();
// Download the compressed file to the local file system
File zipFile = File.createTempFile("example", ".zip");
log.info("zipFile=====" + zipFile.getPath());
minioClient.downloadObject(DownloadObjectArgs.builder().bucket("otatest").object("Template 2.zip").filename(zipFile.getAbsolutePath()).build());
// unzip files
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();
// Delete the local compressed file
zipFile.delete();
} catch (Exception e) {<!-- -->
e.printStackTrace();
}
}
3: Obtain the compressed file stream (this method is faster than downloading compressed files), decompress the files inside, and download them to the specified location on the local server. Note that this download method is very slow, and you need to pay attention to whether the downloaded folder directory is Yes, otherwise it will report an error
@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("template.zip").build())) {<!-- -->
System.out.println(System.currentTimeMillis() - s);
// Create ZipInputStream for reading ZIP files
zis = new ZipInputStream(is, Charset. forName("UTF-8"));
// loop through all entries in the ZIP file
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {<!-- -->
// If the current entry is a directory, create the directory
if (entry.isDirectory()) {<!-- -->
new File(entry. getName()). mkdirs();
} else {<!-- -->
// If the current entry is a file, decompress the file
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();
//delete the downloaded file
Files.delete(Paths.get(StringUtils.getFilename(entry.getName())));
}
}
} catch (Exception e) {<!-- -->
e.printStackTrace();
} finally {<!-- -->
// Close the ZipInputStream
if (zis != null) {<!-- -->
zis. close();
}
}
}
4: Obtain the compressed file stream (this method is faster than downloading compressed files), obtain each file stream in the compressed file, perform operations, and upload to 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("template.zip").build())) {<!-- -->
// Create ZipInputStream for reading ZIP files
zis = new ZipInputStream(is, Charset. forName("GBK"));
// loop through all entries in the ZIP file
ZipEntry entry;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
while ((entry = zis.getNextEntry()) != null) {<!-- -->
// If the current entry is a directory, create the directory
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 {<!-- -->
// Close the ZipInputStream
if (zis != null) {<!-- -->
zis. close();
}
}
}
5: The specified file can be downloaded from the compressed file
@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("template.zip").build());
// Create ZipInputStream for reading ZIP files
zipIn = new ZipInputStream(is, Charset. forName("GBK"));
// loop through all entries in the ZIP file
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
ZipEntry zipEntry;
//Query the download name, which can be an array so that multiple files can be downloaded, but the logic code needs to be modified
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: Obtain the compressed file stream (this method is faster than downloading compressed files), obtain each file stream in the compressed file, perform operations, and upload to minio. This method is recommended and the fastest
@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("template.zip").build());
// Create ZipInputStream for reading ZIP files
zipIn = new ZipInputStream(is, Charset. forName("GBK"));
// loop through all entries in the ZIP file
ZipEntry zipEntry;
//Query the download name, which can be an array so that multiple files can be downloaded, but the logic code needs to be modified
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();
}
}