java.util.zip类 ZipFile

java.util.zip
类 ZipFile

java.lang.Object
  继承者 java.util.zip.ZipFile
直接已知子类:
JarFile

public class ZipFile
    
    
     
     extends Object
    
    

此类用于从 ZIP 文件读取条目。

除非另有说明,将 null 参数传入此类中的构造方法或方法将导致抛出 NullPointerException。


字段摘要
static intCENATT
           
static intCENATX
           
static intCENCOM
           
static intCENCRC
           
static intCENDSK
           
static intCENEXT
           
static intCENFLG
           
static intCENHDR
           
static intCENHOW
           
static intCENLEN
           
static intCENNAM
           
static intCENOFF
           
static longCENSIG
           
static intCENSIZ
           
static intCENTIM
           
static intCENVEM
           
static intCENVER
           
static intENDCOM
           
static intENDHDR
           
static intENDOFF
           
static longENDSIG
           
static intENDSIZ
           
static intENDSUB
           
static intENDTOT
           
static intEXTCRC
           
static intEXTHDR
           
static intEXTLEN
           
static longEXTSIG
           
static intEXTSIZ
           
static intLOCCRC
           
static intLOCEXT
           
static intLOCFLG
           
static intLOCHDR
           
static intLOCHOW
           
static intLOCLEN
           
static intLOCNAM
           
static longLOCSIG
           
static intLOCSIZ
           
static intLOCTIM
           
static intLOCVER
           
static intOPEN_DELETE
          打开 ZIP 文件并将其标记为删除的模式标志。
static intOPEN_READ
          打开 ZIP 文件进行阅读的模式标志。
 
构造方法摘要
ZipFile(File file)
          打开供阅读的 ZIP 文件,由指定的 File 对象给出。
ZipFile(File file, int mode)
          打开新的 ZipFile 以使用指定模式从指定 File 对象读取。
ZipFile(String name)
          打开 ZIP 文件进行阅读。
 
方法摘要
 voidclose()
          关闭 ZIP 文件。
 Enumeration<? extends ZipEntry>entries()
          返回 ZIP 文件条目的枚举。
protected  voidfinalize()
          确保不再引用此 ZIP 文件时调用它的 close 方法。
 ZipEntrygetEntry(String name)
          返回指定名称的 ZIP 文件条目;如果未找到,则返回 null。
 InputStreamgetInputStream(ZipEntry entry)
          返回输入流以读取指定 ZIP 文件条目的内容。
 StringgetName()
          返回 ZIP 文件的路径名。
 intsize()
          返回 ZIP 文件中的条目数。
 
从类 java.lang.Object 继承的方法
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

OPEN_READ

public static final int OPEN_READ
打开 ZIP 文件进行阅读的模式标志。

另请参见:
常量字段值

OPEN_DELETE

public static final int OPEN_DELETE
打开 ZIP 文件并将其标记为删除的模式标志。在打开文件和关闭文件之间的某个时间,该文件将被删除,但是其内容仍可通过 ZipFile 对象访问,直到调用关闭方法或虚拟机退出为止。

另请参见:
常量字段值

LOCSIG

public static final long LOCSIG
另请参见:
常量字段值

EXTSIG

public static final long EXTSIG
另请参见:
常量字段值

CENSIG

public static final long CENSIG
另请参见:
常量字段值

ENDSIG

public static final long ENDSIG
另请参见:
常量字段值

LOCHDR

public static final int LOCHDR
另请参见:
常量字段值

EXTHDR

public static final int EXTHDR
另请参见:
常量字段值

CENHDR

public static final int CENHDR
另请参见:
常量字段值

ENDHDR

public static final int ENDHDR
另请参见:
常量字段值

LOCVER

public static final int LOCVER
另请参见:
常量字段值

LOCFLG

public static final int LOCFLG
另请参见:
常量字段值

LOCHOW

public static final int LOCHOW
另请参见:
常量字段值

LOCTIM

public static final int LOCTIM
另请参见:
常量字段值

LOCCRC

public static final int LOCCRC
另请参见:
常量字段值

LOCSIZ

public static final int LOCSIZ
另请参见:
常量字段值

LOCLEN

public static final int LOCLEN
另请参见:
常量字段值

LOCNAM

public static final int LOCNAM
另请参见:
常量字段值

LOCEXT

public static final int LOCEXT
另请参见:
常量字段值

EXTCRC

public static final int EXTCRC
另请参见:
常量字段值

EXTSIZ

public static final int EXTSIZ
另请参见:
常量字段值

EXTLEN

public static final int EXTLEN
另请参见:
常量字段值

CENVEM

public static final int CENVEM
另请参见:
常量字段值

CENVER

public static final int CENVER
另请参见:
常量字段值

CENFLG

public static final int CENFLG
另请参见:
常量字段值

CENHOW

public static final int CENHOW
另请参见:
常量字段值

CENTIM

public static final int CENTIM
另请参见:
常量字段值

CENCRC

public static final int CENCRC
另请参见:
常量字段值

CENSIZ

public static final int CENSIZ
另请参见:
常量字段值

CENLEN

public static final int CENLEN
另请参见:
常量字段值

CENNAM

public static final int CENNAM
另请参见:
常量字段值

CENEXT

public static final int CENEXT
另请参见:
常量字段值

CENCOM

public static final int CENCOM
另请参见:
常量字段值

CENDSK

public static final int CENDSK
另请参见:
常量字段值

CENATT

public static final int CENATT
另请参见:
常量字段值

CENATX

public static final int CENATX
另请参见:
常量字段值

CENOFF

public static final int CENOFF
另请参见:
常量字段值

ENDSUB

public static final int ENDSUB
另请参见:
常量字段值

ENDTOT

public static final int ENDTOT
另请参见:
常量字段值

ENDSIZ

public static final int ENDSIZ
另请参见:
常量字段值

ENDOFF

public static final int ENDOFF
另请参见:
常量字段值

ENDCOM

public static final int ENDCOM
另请参见:
常量字段值
构造方法详细信息

ZipFile

public ZipFile(String name)
        throws IOException
打开 ZIP 文件进行阅读。

首先,如果存在安全管理器,则使用 name 作为其参数调用其 checkRead 方法,以确保允许阅读。

参数:
name - ZIP 文件的名称
抛出:
ZipException - 如果发生 ZIP 格式错误
IOException - 如果发生 I/O 错误
SecurityException - 如果存在安全管理器,并且其 checkRead 方法不允许对该文件进行阅读访问。
另请参见:
SecurityManager.checkRead(java.lang.String)

ZipFile

public ZipFile(File file,
               int mode)
        throws IOException
打开新的 ZipFile 以使用指定模式从指定 File 对象读取。模式参数必须为 OPEN_READOPEN_READ | OPEN_DELETE

首先,如果存在安全管理器,则使用 name 作为其参数调用其 checkRead 方法,以确保允许阅读。

参数:
file - 要打开进行阅读的 ZIP 文件
mode - 要用于打开文件的模式
抛出:
ZipException - 如果发生 ZIP 格式错误
IOException - 如果发生 I/O 错误
SecurityException - 如果存在安全管理器,并且其 checkRead 方法不允许对该文件进行读取访问,或者当设置了 OPEN_DELETE 标志时,其 checkDelete 方法不允许删除该文件。
IllegalArgumentException - 如果 mode 参数无效
从以下版本开始:
1.3
另请参见:
SecurityManager.checkRead(java.lang.String)

ZipFile

public ZipFile(File file)
        throws ZipException,
               IOException
打开供阅读的 ZIP 文件,由指定的 File 对象给出。

参数:
file - 要打开供阅读的 ZIP 文件
抛出:
ZipException - 如果发生 ZIP 错误
IOException - 如果发生 I/O 错误
方法详细信息

getEntry

public ZipEntry getEntry(String name)
返回指定名称的 ZIP 文件条目;如果未找到,则返回 null。

参数:
name - 条目名称
返回:
ZIP 文件条目;如果未找到,则返回 null
抛出:
IllegalStateException - 如果已关闭该 ZIP

getInputStream

public InputStream getInputStream(ZipEntry entry)
                           throws IOException
返回输入流以读取指定 ZIP 文件条目的内容。

关闭此 ZIP 文件将依次关闭调用此方法返回的所有输入流。

参数:
entry - ZIP 文件条目
返回:
读取指定 ZIP 文件条目内容的输入流。
抛出:
ZipException - 如果发生 ZIP 格式错误
IOException - 如果发生 I/O 错误
IllegalStateException - 如果已关闭 ZIP 文件

getName

public String getName()
返回 ZIP 文件的路径名。

返回:
ZIP 文件的路径名

entries

public Enumeration<? extends ZipEntry> entries()
返回 ZIP 文件条目的枚举。

返回:
ZIP 文件条目的枚举
抛出:
IllegalStateException - 如果已关闭 ZIP 文件

size

public int size()
返回 ZIP 文件中的条目数。

返回:
ZIP 文件中的条目数
抛出:
IllegalStateException - 如果已关闭 ZIP 文件

close

public void close()
           throws IOException
关闭 ZIP 文件。

关闭此 ZIP 文件将关闭以前调用 getInputStream 方法返回的所有输入流。

抛出:
IOException - 如果发生 I/O 错误

finalize

protected void finalize()
                 throws IOException
确保不再引用此 ZIP 文件时调用它的 close 方法。

由于不确定 GC 何时调用此方法,因此强烈建议应用程序一完成访问此 ZipFile 就调用 close 方法。这可防止不定期地保持系统资源。

覆盖:
Object 中的 finalize
抛出:
IOException - 如果发生 I/O 错误
另请参见:
close()
出现 "java.util.zip.ZipException: no current ZIP entry" 错误通常是因为在创建 ZIP 文件时没有正确设置 ZIP 文件的当前条目。 在上述示例中,确保在将文件添加到 ZIP 文件中时,使用正确的 `putNextEntry` 方法来设置当前的 ZIP 条目。以下是修改后的示例代码: ```java import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.*; import java.io.*; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; 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); File zipFile = new File("path/to/save/zip/file.zip"); try (ZipOutputStream zipOutputStream = new ZipOutputStream(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 文件中 zipOutputStream.putNextEntry(new ZipEntry(s3Object.key())); zipOutputStream.write(responseBytes.asByteArray()); zipOutputStream.closeEntry(); } } } } } ``` 确保适当地调用 `zipOutputStream.putNextEntry` 来设置当前的 ZIP 条目,然后将文件内容写入 ZIP 文件中。这样可以避免 "no current ZIP entry" 错误。 如果你仍然遇到问题,请检查你的代码并确保正确设置了 ZIP 条目。另外,还要注意防止尝试使用空的 ZIP 条目进行写入操作。 希望这能解决你的问题!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值