Adding Files to Zip File System of Java

Create Java Zip File Using FileSystem Object

1. Introduction Java Zip File & Java File System

In this example, First, we will create an empty Java Zip File and then add multiple text files to the Zip content. While article progress, we will learn what is Zip FileSystem and Windows File System. Then, we will perform the File Copy from Windows File System to Zip File System there by creating a Zip Cabinet Archive with two text files in it.

The Default File System is the one in which windows organizes the file in a directory tree structure. In our example, we will create a new file system for Zip Cabinet Achieve. Then we will copy the file from Default File System to Zip File System. OK. We will get more picture when we are progressing through the Example.

2. Creating Empty Java Zip File & URI

Before we move into the Example, we will import the required packages. The package inclusion is in the below code:

Now look at the code example given below which creates a zip file and places that in the applications working directory. In our example, we will create a zip file in the same file location where this java program exists.

Creating an Empty Zip file in Java
Creating an Empty Zip file in Java

2.1 Creating Zip Properties Using Map Class

In the above code, first, we are creating the Java Map Class instance called zipSysProps (Marked as 1). A Map is a ‘Key-Value Pair’ and we can retrieve value when a key is given. In our case, we populate the Map zipSysProps with only one key-value pair by calling the put() method of the Map Class. The key is ‘create’ and the value is ‘true’. The Map that we created will acts as Zip File property which is required while we make a call to the Zip creation API.

2.2 The URI (Unique Resource Identifier)

OK. Zip property is ready. Where do we want to create the Zip file? What is the name of the file? We can give this information to Zip creation API through the URI instance. An URI is “Unique Resource Identifier” (URI) which is used in java to locate a resource. A resource can be a Web URL, A Network Socket, A Remote Java Object, etc. In our case, the resource is a Zip file.

The path to our zip file is retrieved by making a call to the get()  method of the Paths factory class. The get method returns Windowspath object (Represented internally by java) and it is stored in the Path interface. Note that the path includes the Zip file name which we pass as a parameter to the get() method (Marked as 2). Once we have the path to ZIP File, we are creating the URI object and storing that in a reference zip_uri (Marked as 3). The first parameter to the URI constructor defines the URI type. In our case, it is Zip Content (jar scheme) and the second parameter tells the path to the Zip location.

2.3 Creating Java Zip File System by making use of URI

The zip in java acts as a separate file system called ‘Zip File System‘ and it can have its own directory structure in it. Using the ‘ FileSystems‘ factory class, we are making a call to the newFileSystem() method to set up the Zip file. The first parameter is the URI which tells Zip File location in the ‘Windows File System’ and the second parameter is the property of the Zip passed as a Map. This property asks to create the Zip file in the path specified by the URI. We store the method return in the FileSystem reference ZipFileSystem which we can use later to add the files to it.

If we run the program at this stage, you will get an empty Zip file in the folder in which the Main class resides.

3. Adding Multiple Text Files to The Empty Java Zip File

In the previous section, we created our zip file. But, the zip file has nothing in it. Now, let us place two text files inside the already create empty zip file. We are not going to create the text files through this example program. So, create two text file named SampleDoc-01.txt and SampleDoc-02.txt. After that we have to add some text contents in both the files and save both. Now, have a look at the below code:

Adding Files to Zip File System of Java
Adding Files to Zip File System of Java

3.1 The Source – Window File Location

The two text files that we created are in the Windows File System. The path of these files in the Windows File Systems is retrieved by making call to the Factory method Paths.get(). Now, we have source location of these files in the Path references called Source1, and Source2 (Marked as 1). Each for these files will have a particular location in side the Zip file.

3.2 Destination of the Java Zip File System

Note that we already created the Zip File (an instance of FileSystem) and stored that in the reference called ZipFileSystem. Using that reference, we are getting the path for the text files inside the Zip File by calling the getPath() method. Now, we have both source and destination locations (Marked as 2). Source location is the path of the Text File in the Windows File System and destination is also path but in Zip File System.

3.3 Copy Files from Windows File System to ZIP File System

After we define source and destination, we make a call to the Files.Copy() method to add content of the zip file. The StandardCopyOption.REPLACE_EXISTING  constant option passed to the copy method will replace the existing file (Marked as 3). The ZipFileSystem is closed at the end (Marked as 4).

4. Complete Code Example

Below is the complete code example:

 

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.