Presentation is loading. Please wait.

Presentation is loading. Please wait.

Field Trip #34 Creating and Reading Zip Files in Java By Keith Lynn.

Similar presentations


Presentation on theme: "Field Trip #34 Creating and Reading Zip Files in Java By Keith Lynn."— Presentation transcript:

1 Field Trip #34 Creating and Reading Zip Files in Java By Keith Lynn

2 java.util.zip The java.util.zip package contains classes that allow us to both read and write zip files A zip file is a file which supports lossless data compression It was very essential when hard drives were small Now, not only does the zip file allow us to save space, but also allows us to organize files

3 ZipEntry In order to create a zip file, we create one or more ZipEntrys When we create a ZipEntry, we typically specify the name of the next file These are the names we see if we look at the contents of the file The ZipEntry does not contain the contents of the file

4 Loading the contents of a file We can think of a file as a collection of bytes Even if the file contains text, we can still think of it as bytes In order to maintain the integrity of the file, we want to make sure we preserve the bytes of the file In order to do this, we can make use of a FileInputStream

5 Streams A stream can be thought of a flow of elements When we want to read the bytes of a file, we can use a flow of bytes The FileInputStream will allow us to make a connection to a file to read its bytes, and the BufferedInputStream allows us to read efficiently from it The read method in BufferedInputStream will allow us to read a certain amount of bytes at a time from the file until we read them all

6 ZipOutputStream When we want to create the zip file, we will first create an instance of a FileOutputStream This will create an output stream that will let us write bytes to a file In order to write a zip file, we then create a ZipOutputStream using that basic connection When we write to the ZipOutputStream, it will properly place ZipEntries and file content in the zip file When we are done, we want to make sure we close the ZipOutputStream

7 ZipInputStream To read in a zip file, we first create an instance of a FileInputStream and attach it to the file This allows us to read bytes from the file We then create a ZipInputStream using the FileInputStream This file allows us to properly retrieve ZipEntries and file contents

8 JFrame To facilitate creating and reading zip files, we will create an instance of a JFrame The JFrame is a graphical interface The JFrame allows us to place a menu bar on the screen We will place menus that allow the user to specify what directory to place in the zip file and what zip file to read

9 JTextArea The JTextArea is a graphical component that allows us to place a large amount of text on the screen We will typically place the JTextArea in a JScrollPane so that if there is more text that we want to place in the text area, scrollbars will appear Depending on the layout manager used in the JFrame, we will typically need to specify the preferred size of the JScrollPane

10 Events In order to detect an event, we must create an event listener There is a package java.awt.event that contains interfaces we can use to detect and act on events An interface is a special Java source file that contains a list of methods to be implemented by the user When a user implements the interface, then it becomes the listener

11 Detecting and acting on selecting menus To detect when a user selects a menu item, we attach an ActionListener to the menu item The ActionListener interface contains the method actionPerformed which accepts an ActionEvent parameter We can detect the source of an event with the method getSource() and the label of the source with the method getActionCommand()


Download ppt "Field Trip #34 Creating and Reading Zip Files in Java By Keith Lynn."

Similar presentations


Ads by Google