Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

Zip Or Gnu-zip

Discussion in 'Java Update' started by Joe, 23/3/19.

  1. Joe

    Joe Moderator

    Chao cac ban

    Everyone knows what gzip is. It's the GNU ZIP or GZIP for short. ZIP was the development of PKWARE and based on the LZ77 and LZ78 Algorithm. ZIP wasn't free in the 80s and computers were usually in short with space. Hence ZIP was the most desirable software to compress data in order to save space on the disk. Either to pay for the fee-based ZIP Tool, or to develop for yourself your own ZIP-similar Software-tool. Jean-loup Gailly and Mark Adler took the challenge to heart and developed the gzip -exactly as the Unix philosophy: "Do One Thing and Do It Well". It's the GNU ZIP Project.

    Today, even a tiny smartphone has nowadays more "disk spaces" than an IBM Mainframe System/390 of the 90s (click HERE to see how "big" an IBM Mainframe could be in the 1990s). The need of ZIP decreases and the tools (in form of APIs) become free. However, gzip is still omnipresent everywhere and in our daily Web activity. I am sure that every IT man has once seen this HTTP Request:
    PHP:
    GET ... HTTP/1.1
    ...
    Accept-Encodinggzipdeflatesdch
    Accept
    -Languageen-US,en;q=0.8
    ...
    The line "Accept-Encoding: gzip, deflate, sdch" says it all: without gzip the web will be snailed down dramatically and then stands still... like the traffic in LA or in Saigon. JAVA, for example, provides the ZIP APIs as standard in the package java.util.zip. Instead of googling for a free ZIP tool why you, an IT developer, won't try to build yourself a ZIPPING tool? With the available JAVA APIs it's a piece of cake to design a fancy ZIP or GZIP app and to impress your friends. However, gzip was designed to only compress files, not directories. Why so? It's because of the patent problems with PKWARE. And that's the real challenge for an IT man like YOU: 2 apps that base on zip and gzip technology and work as exactly as any ZIP-tool does.

    You got Problems? Hm...

    Well, I show you how to develop two fancy apps that look exactly the same, but the one works with ZIP APIs and the other with GZIP APIs. JAVA GUI is either SWING or JFX. SWING is fine, and popular among the oldies. JFX is more modern and is beloved by the newbies. Taste is usually individual and non-discussable. However, JavaFX offers the developers more features and possibilities than SWING: styling, coloring, animation and shapes. Further one can "follow" the trend "MVC" (Model-View-Controller) without any problem. In my blog here I won't do that because the source of each app is less than 500 lines and there's no reason to complicate the work with "MVC". However, the JFX apps (ZIP.java and JZIP.java) are implemented as the lightweight MVC apps which let its users modify the Cascading Style Sheets file css.css to suit their personal "taste".

    ARCHITECTURE

    [​IMG]
    Fig.1

    Fig.1 shows the structure of our apps (for both ZIP and JZIP). The naming JZIP is to unravel the confusion between gzip (for a file) and jzip (with gzip for more files/directories.) Btw, J stands for Java or, if you so will, for Joe. The JavaFX layout for both apps is the same -except the naming of some JFX Buttons (ZIP or JZIP). The structural components of the ZIP/JZIP engine are:

    • create: create a (J)ZIP file with the selected files (with abs. path). There are two options for a zip/jzip creation: the zip/jzip must be either created or partially replaced/appended with the selected files.
    • analyze & display: The LIST or SHOW option analyzes the selected zip/jzip file for its validity and lists all zipped/jzipped entries (LIST). For SHOW: if an entry is selected (left pane) its content will be displayed on the right pane.
    • analyze & inflate: UNZIP/UNJZIP checks firstly for the zip/jzip validity, then shows an entry-list for UNZIP/UNJZIP. Only the selected entries are inflated and created in the selected ROOT directory (incl. their path). If nothing was selected and UNZIP/UNJZIP button was pressed all entries are selected for UNZIP/UNJZIP.

    The ZIP engine uses all standard APIs of the java.util.zip package: ZipInputStream, ZipOutputStream and ZipEntry.

    The JZIP engine uses only TWO standard APIs of the java.util.zip package: GZIPInputStream, GZIPOutputStream. Instead of ZipEntry JZIP engine provides its own methods (joe's propriety) to sort out the JZIP-Entries and their contents: listEntry(), entryName(), nextEntry(), rawContent(), rawEntry().

    The ZIP source looks a bit "shorter" than the JZIP, but in reality it's larger if the ZipEntry's methods are included into the source (as all the mentioned JZIP methods with JZIP.) The following table shows you the efficiency of JZIP (gzip) versus ZIP (zip):
    PHP:
    20.03.2019  17:40               827 css.css
    19.03.2019  10
    :23               115 JMANIFEST.MF
    20.03.2019  17
    :41               993 joeDialog.css
    21.03.2019  13
    :43            19.518 JZIP.java
    18.03.2019  13
    :37               114 MANIFEST.MF
    21.03.2019  13
    :41            18.868 ZIP.java
    17.03.2019  10
    :23            20.362 zip.jpg
    -----------------------------------------------
    Total                        60.797
    -----------------------------------------------
    21.03.2019  12:19            15.045 zip.zip
    21.03.2019  12
    :17            10.468 jzip.jzip
    -----------------------------------------------
    zip.zip:    reduced 45.752  or  75.25 %
    jzip.jzip:  reduced 50.329  or  82.78 %
    [​IMG]
    [​IMG]

    Note: the zipped result of ZIP.java is compatible with all commercial ZIP tools.
    The sources JZIP.java and ZIP.java plus other sources (image, css file) can be downloaded HEREUNDER. The file jzip.txt should be renamed back to jzip.jzip to avoid confusion with a plain text file :D And if you want to make an executable jar file you have to create a Manifest file as following (here with JZIP):

    1. download the zip.zip and jzip.txt
    2. unzip zip.zip and compile both files (javac -g:none -d ./classes *.java)
    3. move to the directory classes and run the jar cmd (see the followings).
    4. finally you can run java -jar jzip.jar and see how the content of jzip.jzip looks like.

    manifest.txt file
    PHP:
     
    Manifest
    -Version1.0
    Permissions
    sandbox
    JavaFX
    -Version8.0
    Created
    -ByJoe
    Main
    -Class: JZIP
    then run on a CMD-Windows with the directory where the JZIP.class, zip.jpg, css.css are stored:
    PHP:
     
    jar 
    -cvfm jzip.jar manifest.txt JZIP.class css.css zip.jpg out.txt
    That's all, folk! Enjoy the JZIP app or the ZIP.

    Joe[/B]
     

    Attached Files:

    Last edited: 2/4/19
    tranhuyvc likes this.

Chia sẻ trang này

Loading...