您的位置:首页 >> 编程开发 >> Java >> JDK核心API >> 正文
RSS
 

Compressing and Decompressing Data using JavaTM APIs

http://www.rdxx.com 02年02月25日 12:12 Blog 我要投稿

关键词: ATM , SSI , API , COM , TM , Java

Compressing and Decompressing Data using JavaTM APIs

by Qusay H. Mahmoud
with contributions from Konstantin Kladko

February 2002

Many sources of information contain redundant data or data that adds little to the stored information. This results in tremendous amounts of data being transferred between client and server applications or computers in general. The obvious solution to the problems of data storage and information transfer is to install additional storage devices and expand existing communication facilities. To do so, however, requires an increase in an organization's operating costs. One method to alleviate a portion of data storage and information transfer is through the representation of data by more efficient code. This article presents a brief introduction to data compression and decompression, and shows how to compress and decompress data, efficiently and conveniently, from within your JavaTM applications using the java.util.zip package.

While it is possible to compress and decompress data using tools such as WinZip, gzip, and Java ARchive (or jar), these tools are used as standalone applications. It is possible to invoke these tools from your Java applications, but this is not a straightforward approach and not an efficient solution. This is especially true if you wish to compress and decompress data on the fly (before transferring it to a remote machine for example). This article:

  • Gives you a brief overview of data compression
  • Describes the java.util.zip package
  • Shows how to use this package to compress and decompress data
  • Shows how to compress and decompress serialized objects to save disk space
  • Shows how to compress and decompress data on the fly to improve the performance of client/server applications

Overview of Data Compression

The simplest type of redundancy in a file is the repetition of characters. For example, consider the following string:

BBBBHHDDXXXXKKKKWWZZZZ

This string can be encoded more compactly by replacing each repeated string of characters by a single instance of the repeated character and a number that represents the number of times it is repeated. The earlier string can be encoded as follows:

4B2H2D4X4K2W4Z

Here "4B" means four B's, and 2H means two H's, and so on. Compressing a string in this way is called run-length encoding.

As another example, consider the storage of a rectangular image. As a single color bitmapped image, it can be stored as shown in Figure 1.

Figure 1: A bitmap with information for run-length encoding

Another approach might be to store the image as a graphics metafile:

Rectangle 11, 3, 20, 5

This says, the rectangle starts at coordinate (11, 3) of width 20 and length 5 pixels.

The rectangular image can be compressed with run-length encoding by counting identical bits as follows:

0, 400, 400,10 1,20 0,100,10 1,1 0,18 1,1 0,100,10 1,1 0,18 1,1 0,100,10 1,1 0,18 1,1 0,100,10 1,20 0,100,40
9 7 3 1 2 3 4 5 6 7 8 9 10 4 8 :

 
 
标签: ATM , SSI , API , COM , TM , Java 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站