我遇到了一种情况,我需要打开驻留在 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;
}
Run Code Online (Sandbox Code Playgroud)
以前,我确实尝试使用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);
}
Run Code Online (Sandbox Code Playgroud)
有人遇到过这种情况吗?请给我建议谢谢:)
如果您想立即使用 zip 文件而不先将其保存到临时文件,您可以使用java.util.zip.ZipInputStream:
import java.util.zip.ZipInputStream;
S3ObjectInputStream is = obj.getObjectContent();
ZipInputStream zis = new ZipInputStream(is);
Run Code Online (Sandbox Code Playgroud)
从那里您可以阅读 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 { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
您无需担心以这种方式存储临时文件。
| 归档时间: |
|
| 查看次数: |
4450 次 |
| 最近记录: |