Overview
The article will demonstrate how to use the zip component in Secure iNet Factory to zip/unzip files in Java.
Code Example
Download GZIPExample.java
01 import java.io.*;
02 import com.jscape.zip.*;
03
04 public class GZIPExample implements ArchiveListener
05 {
06 public static void main(String[] args)
07 {
08 Gzip gzip = new Gzip();
09 Zip zip = new Zip();
10
11 // Both zip and Gzip in com.jscape.zip can register with an instance of
12 // ArchiveListener to receive events.
13
14 // Register an instance of ArchiveListener tp GZIP instance
15 // so the GZIP instance can receive events.
16 gzip.addArchiveListener(new GZIPExample());
17
18 // Register an instance of ArchiveListener tp ZIP instance
19 // so the ZIP instance can receive events.
20 zip.addArchiveListener(new GZIPExample());
21
22 try
23 {
24 // variables to use the GZIP extension.
25 String inputFile = "TestFile";
26 String outputFile = "TestFile.gz";
27
28 // compress an existing file to the specified output file
29 // Argument 1 : Name of the input file.
30 // Argument 2 : Name of the output file
31 gzip.compress(new File(inputFile), new File(outputFile));
32
33 // decompress an existing archive
34 // Argument 1 : Name of the existing archive
35 // Argument 2 : Destination directory where the uncompressed file will be located.
36 gzip.decompress(new File(outputFile), new File("E:\\"));
37
38 // change the variables to use the ZIP extension.
39 inputFile = "TestFile";
40 outputFile = "TestFile.zip";
41
42 // compress an existing file to the specified output file
43 // Argument 1 : Name of the input file.
44 // Argument 2 : Name of the output file
45 zip.compress(new File(inputFile), new File(outputFile));
46
47 // decompress an existing archive
48 // Argument 1 : Name of the existing archive
49 // Argument 2 : Destination directory where the uncompressed file will be located.
50 zip.decompress(new File(outputFile), new File("E:\\"));
51 }
52 catch (ArchiveException e)
53 {
54 e.printStackTrace();
55 }
56 }
57
58 // this method is invoked when a file is compressed.
59 public void compressed(ArchiveCompressedEvent argument)
60 {
61 // print out the source object as a string so we know from which
62 // type of object the event came from
63 System.out.println(argument.getSource().toString());
64
65 File file = argument.getFile();
66 System.out.println("Compressed file name : " + file.getName());
67 System.out.println("Compressed file absolute path : " + file.getAbsolutePath());
68 System.out.println();
69 }
70
71 // this method is invoked when a file is uncompressed.
72 public void decompressed(ArchiveDecompressedEvent argument)
73 {
74 // print out the source object as a string so we know from which
75 // type of object the event came from
76 System.out.println(argument.getSource().toString());
77
78 File file = argument.getFile();
79 System.out.println("Decompressed file name : " + file.getName());
80 System.out.println("Decompressed file absolute path : " + file.getAbsolutePath());
81 System.out.println();
82 }
83 }
Lines 1-2 : Necessary imports. Lines 8-9 : Create instances of Gzip and Zip. Lines 31/36 : Compress / Decompress files using the Gzip instance. Lines 45/50 : Compress / Decompress files using the zip instance. Variables on lines 30/40 need to be changed to the correct file extension. Lines 59-60 : Compress event is received here. Lines 72-82 : Decompress event is received here.
This code assumes you have a file named "TestFile" under the program directory and you have hard drive labeled "E:\\" on your system. Please change these variable values before running the program to reflect the correct file-name and location.
|
Comments