Java read from zip file

Содержание
  1. Java ZipInputStream
  2. Java ZipInputStream
  3. ZIP
  4. ZipInputStream constructors
  5. ZipInputStream getNextEntry
  6. Java read ZIP example
  7. Java decompress ZIP example
  8. Author
  9. Java read from zip file
  10. Field Summary
  11. Fields inherited from class java.util.zip.InflaterInputStream
  12. Fields inherited from class java.io.FilterInputStream
  13. Constructor Summary
  14. Method Summary
  15. Methods inherited from class java.util.zip.InflaterInputStream
  16. Methods inherited from class java.io.FilterInputStream
  17. Methods inherited from class java.lang.Object
  18. Field Detail
  19. LOCSIG
  20. EXTSIG
  21. CENSIG
  22. ENDSIG
  23. LOCHDR
  24. EXTHDR
  25. CENHDR
  26. ENDHDR
  27. LOCVER
  28. LOCFLG
  29. LOCHOW
  30. LOCTIM
  31. LOCCRC
  32. LOCSIZ
  33. LOCLEN
  34. LOCNAM
  35. LOCEXT
  36. EXTCRC
  37. EXTSIZ
  38. EXTLEN
  39. CENVEM
  40. CENVER
  41. CENFLG
  42. CENHOW
  43. CENTIM
  44. CENCRC
  45. CENSIZ
  46. CENLEN
  47. CENNAM
  48. CENEXT
  49. CENCOM
  50. CENDSK
  51. CENATT
  52. CENATX
  53. CENOFF
  54. ENDSUB
  55. ENDTOT
  56. ENDSIZ
  57. ENDOFF
  58. ENDCOM
  59. Constructor Detail
  60. ZipInputStream
  61. ZipInputStream
  62. Method Detail
  63. getNextEntry
  64. closeEntry
  65. available
  66. read
  67. skip
  68. close
  69. createZipEntry
  70. Class ZipFile
  71. Field Summary
  72. Constructor Summary
  73. Method Summary
  74. Methods declared in class java.lang.Object
  75. Field Details
  76. OPEN_READ
  77. OPEN_DELETE
  78. LOCSIG
  79. EXTSIG
  80. CENSIG
  81. ENDSIG
  82. LOCHDR
  83. EXTHDR
  84. CENHDR
  85. ENDHDR
  86. LOCVER
  87. LOCFLG
  88. LOCHOW
  89. LOCTIM
  90. LOCCRC
  91. LOCSIZ
  92. LOCLEN
  93. LOCNAM
  94. LOCEXT
  95. EXTCRC
  96. EXTSIZ
  97. EXTLEN
  98. CENVEM
  99. CENVER
  100. CENFLG
  101. CENHOW
  102. CENTIM
  103. CENCRC
  104. CENSIZ
  105. CENLEN
  106. CENNAM
  107. CENEXT
  108. CENCOM
  109. CENDSK
  110. CENATT
  111. CENATX
  112. CENOFF
  113. ENDSUB
  114. ENDTOT
  115. ENDSIZ
  116. ENDOFF
  117. ENDCOM
  118. Constructor Details
  119. ZipFile
  120. ZipFile
  121. ZipFile
  122. ZipFile
  123. ZipFile
  124. ZipFile
  125. Method Details
  126. getComment
  127. getEntry
  128. getInputStream
  129. getName
  130. entries
  131. stream
  132. size
  133. close
  134. Class ZipInputStream
  135. Field Summary
  136. Fields declared in class java.util.zip.InflaterInputStream
  137. Fields declared in class java.io.FilterInputStream
  138. Constructor Summary
  139. Method Summary
  140. Methods declared in class java.util.zip.InflaterInputStream
  141. Methods declared in class java.io.FilterInputStream
  142. Methods declared in class java.io.InputStream
  143. Methods declared in class java.lang.Object
  144. Field Details
  145. LOCSIG
  146. EXTSIG
  147. CENSIG
  148. ENDSIG
  149. LOCHDR
  150. EXTHDR
  151. CENHDR
  152. ENDHDR
  153. LOCVER
  154. LOCFLG
  155. LOCHOW
  156. LOCTIM
  157. LOCCRC
  158. LOCSIZ
  159. LOCLEN
  160. LOCNAM
  161. LOCEXT
  162. EXTCRC
  163. EXTSIZ
  164. EXTLEN
  165. CENVEM
  166. CENVER
  167. CENFLG
  168. CENHOW
  169. CENTIM
  170. CENCRC
  171. CENSIZ
  172. CENLEN
  173. CENNAM
  174. CENEXT
  175. CENCOM
  176. CENDSK
  177. CENATT
  178. CENATX
  179. CENOFF
  180. ENDSUB
  181. ENDTOT
  182. ENDSIZ
  183. ENDOFF
  184. ENDCOM
  185. Constructor Details
  186. ZipInputStream
  187. ZipInputStream
  188. Method Details
  189. getNextEntry
  190. closeEntry
  191. available
  192. read
  193. skip
  194. close
  195. createZipEntry
Читайте также:  Html templates law firm

Java ZipInputStream

Java ZipInputStream tutorial shows how to read ZIP files in Java with ZipInputStream .

Java ZipInputStream

ZipInputStream is a Java class that implements an input stream filter for reading files in the ZIP file format. It has support for both compressed and uncompressed entries.

ZIP

ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. Java Archive (JAR) is built on the ZIP format.

ZipInputStream constructors

ZipInputStream has the following constructors:

ZipInputStream(InputStream in) ZipInputStream(InputStream in, Charset charset)

ZipInputStream getNextEntry

The ZipInputStream’s getNextEntry reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Java read ZIP example

The following example reads the contents of a ZIP file.

package com.zetcode; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.time.LocalDate; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class JavaReadZip < private final static Long MILLS_IN_DAY = 86400000L; public static void main(String[] args) throws IOException < String fileName = "src/resources/myfile.zip"; try (FileInputStream fis = new FileInputStream(fileName); BufferedInputStream bis = new BufferedInputStream(fis); ZipInputStream zis = new ZipInputStream(bis)) < ZipEntry ze; while ((ze = zis.getNextEntry()) != null) < System.out.format("File: %s Size: %d last modified: %d", ze.getName(), ze.getSize(), LocalDate.ofEpochDay(ze.getTime() / MILLS_IN_DAY)); >> > >

The example reads the given ZIP file with ZipInputStream and prints its contents to the terminal. We print the file names, their size, and the last modification time.

String fileName = "src/resources/myfile.zip";

The ZIP file is located int src/resources/ directory.

try (FileInputStream fis = new FileInputStream(fileName);

We create a FileInputStream from the file. FileInputStream is used for reading streams of raw bytes.

BufferedInputStream bis = new BufferedInputStream(fis);

For better performance, we pass the FileInputStream into the BufferedInputStream .

ZipInputStream zis = new ZipInputStream(bis)) 

A ZipInputStream is created from the buffered FileInputStream . The try-with-resources closes the streams when they are not needed anymore.

while ((ze = zis.getNextEntry()) != null) 

In a while loop, we go through the entries of the ZIP file with getNextEntry method. It returns null if there are no more entries.

System.out.format("File: %s Size: %d last modified: %d", ze.getName(), ze.getSize(), LocalDate.ofEpochDay(ze.getTime() / MILLS_IN_DAY));

The getName returns the name of the entry, the getSize returns the uncompressed size of the entry, and the getTime returns the last modification time of the entry.

Java decompress ZIP example

In the next example, we decompress a ZIP file in Java.

package com.zetcode; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.file.Path; import java.nio.file.Paths; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class JavaUnzip < public static void main(String args[]) throws Exception < byte[] buffer = new byte[2048]; Path outDir = Paths.get("src/resources/output/"); String zipFileName = "src/resources/myfile.zip"; try (FileInputStream fis = new FileInputStream(zipFileName); BufferedInputStream bis = new BufferedInputStream(fis); ZipInputStream stream = new ZipInputStream(bis)) < ZipEntry entry; while ((entry = stream.getNextEntry()) != null) < Path filePath = outDir.resolve(entry.getName()); try (FileOutputStream fos = new FileOutputStream(filePath.toFile()); BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length)) < int len; while ((len = stream.read(buffer)) >0) < bos.write(buffer, 0, len); >> > > > >

The example uses ZipInputStream to read the contents of the given ZIP file and FileOutputStream and BufferedOutputStream to write the contents into a directory.

Path outDir = Paths.get("src/resources/output/");

This is the directory where we extract the contents of the ZIP file.

while ((entry = stream.getNextEntry()) != null) 

In the first while loop, we go through the entries of the ZIP file.

while ((len = stream.read(buffer)) > 0)

In the second while loop, we read the entries and write them to the output stream.

In this article we have presented the Java ZipInputStream class. We have created two examples to read a ZIP file and to decompress a ZIP file.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

Источник

Java read from zip file

This class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries.

Field Summary

Fields inherited from class java.util.zip.InflaterInputStream

Fields inherited from class java.io.FilterInputStream

Constructor Summary

Method Summary

Methods inherited from class java.util.zip.InflaterInputStream

Methods inherited from class java.io.FilterInputStream

Methods inherited from class java.lang.Object

Field Detail

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

Constructor Detail

ZipInputStream

ZipInputStream

Method Detail

getNextEntry

public ZipEntry getNextEntry() throws IOException

closeEntry

available

Returns 0 after EOF has reached for the current entry data, otherwise always return 1. Programs should not count on this method to return the actual number of bytes that could be read without blocking.

read

Reads from the current ZIP entry into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.

skip

close

createZipEntry

protected ZipEntry createZipEntry(String name)

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Class ZipFile

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

Field Summary

Constructor Summary

Method Summary

Methods declared in class java.lang.Object

Field Details

OPEN_READ

OPEN_DELETE

Mode flag to open a zip file and mark it for deletion. The file will be deleted some time between the moment that it is opened and the moment that it is closed, but its contents will remain accessible via the ZipFile object until either the close method is invoked or the virtual machine exits.

LOCSIG

EXTSIG

CENSIG

ENDSIG

LOCHDR

EXTHDR

CENHDR

ENDHDR

LOCVER

LOCFLG

LOCHOW

LOCTIM

LOCCRC

LOCSIZ

LOCLEN

LOCNAM

LOCEXT

EXTCRC

EXTSIZ

EXTLEN

CENVEM

CENVER

CENFLG

CENHOW

CENTIM

CENCRC

CENSIZ

CENLEN

CENNAM

CENEXT

CENCOM

CENDSK

CENATT

CENATX

CENOFF

ENDSUB

ENDTOT

ENDSIZ

ENDOFF

ENDCOM

Constructor Details

ZipFile

Opens a zip file for reading. First, if there is a security manager, its checkRead method is called with the name argument as its argument to ensure the read is allowed. The UTF-8 charset is used to decode the entry names and comments.

ZipFile

Opens a new ZipFile to read from the specified File object in the specified mode. The mode argument must be either OPEN_READ or OPEN_READ | OPEN_DELETE . First, if there is a security manager, its checkRead method is called with the name argument as its argument to ensure the read is allowed. The UTF-8 charset is used to decode the entry names and comments

ZipFile

Opens a ZIP file for reading given the specified File object. The UTF-8 charset is used to decode the entry names and comments.

ZipFile

Opens a new ZipFile to read from the specified File object in the specified mode. The mode argument must be either OPEN_READ or OPEN_READ | OPEN_DELETE . First, if there is a security manager, its checkRead method is called with the name argument as its argument to ensure the read is allowed.

ZipFile

Opens a zip file for reading. First, if there is a security manager, its checkRead method is called with the name argument as its argument to ensure the read is allowed.

ZipFile

Method Details

getComment

getEntry

getInputStream

Returns an input stream for reading the contents of the specified zip file entry. Closing this ZIP file will, in turn, close all input streams that have been returned by invocations of this method.

getName

entries

stream

Returns an ordered Stream over the ZIP file entries. Entries appear in the Stream in the order they appear in the central directory of the ZIP file.

size

close

Closes the ZIP file. Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream method.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Class ZipInputStream

This class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries.

Field Summary

Fields declared in class java.util.zip.InflaterInputStream

Fields declared in class java.io.FilterInputStream

Constructor Summary

Method Summary

Methods declared in class java.util.zip.InflaterInputStream

Methods declared in class java.io.FilterInputStream

Methods declared in class java.io.InputStream

Methods declared in class java.lang.Object

Field Details

LOCSIG

EXTSIG

CENSIG

ENDSIG

LOCHDR

EXTHDR

CENHDR

ENDHDR

LOCVER

LOCFLG

LOCHOW

LOCTIM

LOCCRC

LOCSIZ

LOCLEN

LOCNAM

LOCEXT

EXTCRC

EXTSIZ

EXTLEN

CENVEM

CENVER

CENFLG

CENHOW

CENTIM

CENCRC

CENSIZ

CENLEN

CENNAM

CENEXT

CENCOM

CENDSK

CENATT

CENATX

CENOFF

ENDSUB

ENDTOT

ENDSIZ

ENDOFF

ENDCOM

Constructor Details

ZipInputStream

ZipInputStream

Method Details

getNextEntry

closeEntry

available

Returns 0 after EOF has reached for the current entry data, otherwise always return 1. Programs should not count on this method to return the actual number of bytes that could be read without blocking.

read

Reads from the current ZIP entry into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.

skip

close

createZipEntry

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Оцените статью