Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet167/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   163   164   165   166   167   168   169   170   ...   245
Bog'liq
Java 17 Recipes

 How It Works
To work with the contents of a .zip archive, create a ZipFile object. A ZipFile object 
can be instantiated, passing the name of a .zip archive to the constructor. After creating 
the object, you access the specified .zip file information. Each ZipFile object contains 
entries representing the directories and files contained within the archive. By iterating 
through the entries, you can obtain information on each compressed file. Each ZipEntry 
instance has the compressed and uncompressed size, the name, and the input stream of 
the uncompressed bytes.
The uncompressed bytes can be read into a byte buffer by generating an 
InputStream, and later (in our case) written to a file. Using the FileStream, it is possible 
to determine how many bytes can be read without blocking the process. Once the 
determined number of bytes has been read, those bytes are written to the output file. 
This process continues until the total number of bytes has been read.
Note reading the entire file into memory may not be a good idea if the file is 
extremely large. If you need to work with a large file, it’s best to first write it in 
an uncompressed format to disk (as in the example) and then open it and load 
it in chunks. If the file you are working on is not large (you can limit the size by 
checking the getSize() method), you can probably load it in memory.
ChApTer 8 InpuT And OuTpuT


328
8-13. Summary
This chapter demonstrated several examples for working with file and network I/O in 
Java. You learned how to serialize files to be stored to disk and how to manipulate a 
host’s file system with the Java APIs. The chapter also covered how to read and write 
property files and perform file compression. Lastly, the chapter touched on the new 
features of the Process API added in Java 9.
ChApTer 8 InpuT And OuTpuT


329
© Josh Juneau, Luciano Manelli 2022 
J. Juneau and L. Manelli, Java 17 Recipes
https://doi.org/10.1007/978-1-4842-7963-2_9

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   163   164   165   166   167   168   169   170   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling