gpt4 book ai didi

java - 如何加载驻留在 S3 存储桶中的 zip 文件?

转载 作者:行者123 更新时间:2023-11-30 02:14:51 26 4
gpt4 key购买 nike

我遇到了一种情况,需要打开驻留在 S3 存储桶中的 zip 文件。到目前为止我的代码如下:

public ZipFile readZipFile(String name) throws Exception {
GetObjectRequest req = new GetObjectRequest(settings.getAwsS3BatchRecogInBucketName(), name);
S3Object obj = s3Client.getObject(req);
S3ObjectInputStream is = obj.getObjectContent();

/******************************
* HOW TO DO
******************************/
return null;
}

以前,我确实尝试过使用 File.createTempFile 函数创建临时文件对象,但总是遇到无法创建 File 对象的问题。我之前的尝试如下:

public ZipFile readZipFile(String name) throws Exception {
GetObjectRequest req = new GetObjectRequest(settings.getAwsS3BatchRecogInBucketName(), name);
S3Object obj = s3Client.getObject(req);
S3ObjectInputStream is = obj.getObjectContent();

File temp = File.createTempFile(name, "");
temp.setWritable(true);
FileOutputStream fos = new FileOutputStream(temp);
fos.write(IOUtils.toByteArray(is));
fos.flush();
return new ZipFile(temp);
}

有人遇到过这种情况吗?请给我建议,谢谢:)

最佳答案

如果您想立即使用 zip 文件而不先将其保存到临时文件,可以使用 java.util.zip.ZipInputStream:

import java.util.zip.ZipInputStream;

S3ObjectInputStream is = obj.getObjectContent();
ZipInputStream zis = new ZipInputStream(is);

从那里您可以阅读 zip 文件的条目,忽略不需要的条目,并使用您需要的条目:

ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
String name = entry.getName();
if (iWantToProcessThisEntry(name)) {
processFile(name, zis);
}
zis.closeEntry();
}

public void processFile(String name, InputStream in) throws IOException { /* ... */ }

您无需担心以这种方式存储临时文件。

关于java - 如何加载驻留在 S3 存储桶中的 zip 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48920289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com