出现 "
java.
util.
zip.
ZipException: no current
ZIP entry" 错误通常是因为在创建
ZIP文件时没有正确设置
ZIP文件的当前条目。
在上述
示例中,确保在将文件添加到
ZIP文件中时,使用正确的 `putNext
Entry`
方法来设置当前的
ZIP条目。以下是修改后的
示例代码:
javaimport software.amazon.awssdk.services.s3.S3Client;import software.amazon.awssdk.services.s3.model.*;importjava.io.*;importjava.time.Instant;importjava.time.temporal.ChronoUnit;importjava.util.zip.Zip Entry;importjava.util.zip.Zip OutputStream;public class S3FileFilterExample {public static void main(String[] args) throws IOException {S3Client s3Client = S3Client.builder().region(Region.YOUR_REGION).credentialsProvider(DefaultCredentialsProvider.create()).build();String bucketName = "your-bucket-name";String folderPath = "your-folder-path";Instant startTime = Instant.now().minus(1, ChronoUnit.DAYS); // 设置起始时间,这里设置为1天前Instant endTime = Instant.now(); // 设置结束时间,这里设置为当前时间ListObjectsV2Request listRequest = ListObjectsV2Request.builder().bucket(bucketName).prefix(folderPath).build();ListObjectsV2Response listResponse = s3Client.listObjectsV2(listRequest);FilezipFile = new File("path/to/save/zip/file.zip");try (Zip OutputStream zip OutputStream= newZip OutputStream(new FileOutputStream(zipFile))) {for (S3Object s3Object : listResponse.contents()) {if (s3Object.lastModified().isAfter(startTime) && s3Object.lastModified().isBefore(endTime)) {GetObjectRequest getRequest = GetObjectRequest.builder().bucket(bucketName).key(s3Object.key()).build();ResponseBytes<GetObjectResponse> responseBytes = s3Client.getObjectAsBytes(getRequest);// 将文件添加到ZIP文件中zip OutputStream.putNextEntry(newZip Entry(s3Object.key()));zip OutputStream.write(responseBytes.asByteArray());zip OutputStream.closeEntry();}}}}}
确保适当地调用 `
zip OutputStream.putNext
Entry` 来设置当前的
ZIP条目,然后将文件内容写入
ZIP文件中。这样可以避免 "no current
ZIP entry" 错误。
如果你仍然遇到问题,请检查你的代码并确保正确设置了
ZIP条目。另外,还要注意防止尝试使用空的
ZIP条目进行写入操作。
希望这能解决你的问题!
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.mushiming.com/mjsbk/15095.html