A kind of data compression method and device
Technical field
The invention belongs to field of data compression, more particularly to a kind of data compression method and device.
Background technology
Data compression refers on the premise of useful information is not lost, group again to be carried out to data according to certain algorithm
Knit, reduce the redundancy of data and the space of storage, the method for improving its transmission, storage and treatment effeciency.Calculated by data compression
After method will need the data for storing, transmitting to be compressed, it is possible to use the more data of less memory space storage, and
During data transfer, Internet resources can be greatly saved, and improve the efficiency of transmission.
At present, when being compressed to data, typically from existing data compression instrument, such as WINRAR, WINZIP,
The instruments such as good pressure, 360 compressions.And the compression algorithm that different tools of compression is used is likely to differ.Therefore, user is worked as
When being compressed using tool of compression A, in different use environments, such as will compress data is activation to other users, or
When the data duplication that person will compress is in miscellaneous equipment, may be due to the difference of tool of compression, it is impossible to which data are carried out effectively
Decompression.Need user that corresponding tool of compression is installed in new equipment, operation is more bothered.
The content of the invention
It is an object of the invention to provide a kind of data compression method and device, to solve prior art due to tool of compression
It is varied, cause the data of compression to effectively adapt to different instruments and decompressed, the problem of user's inconvenient operation.
In a first aspect, the embodiment of the invention provides a kind of data compression method, methods described includes:
Data compression request is received, the data of needs compression is obtained and is compressed the selected compression algorithm of data;
The data for needing compression are compressed according to selected compression algorithm, the data after being compressed;
The corresponding mark of the compression algorithm is added to first specified location in packet header, by the data after the compression and
Packet header generates compressed data packets.
With reference in a first aspect, in the first possible implementation of first aspect, in the reception data compression request,
After the step of obtaining the data that need compression and the selection compression data selected compression algorithm, methods described is also wrapped
Include:
Data to needing compression are circulated redundancy check, generate CRC result;
The CRC result is added to second specified location in the packet header.
With reference in a first aspect, second of first aspect may in implementation, in the reception data compression request,
After the step of obtaining the data that need compression and the selection compression data selected compression algorithm, methods described is also wrapped
Include:
The original length of the length for obtaining the data after compression and the data for needing compression;
The length of the data after the compression is write the 3rd specified location in the packet header, and the number of compression will be needed
According to original length write packet header the 4th specified location.
Second aspect, the embodiment of the invention provides a kind of data decompression method, and methods described includes:
Data decompression request is received, the compression algorithm obtained corresponding to the data after the compression in compressed data packets is corresponding
Mark, and the data after the specified location of compressed data packets obtains compression;
According to the corresponding compression algorithm of the identifier lookup, the data after compression are decompressed according to the compression algorithm
Contracting, the data for being decompressed.
With reference to second aspect, in the first possible implementation of second aspect, described according to the identifier lookup
Data after compression are decompressed by corresponding compression algorithm according to the compression algorithm, the data step for being decompressed
Afterwards, methods described also includes:
The data for decompressing are verified by circulating result method of calibration, the check results of generation decompression data;
Check results by the check results of the decompression data with storage in the specified location of packet header second are compared, root
The integrality of the data of the decompression is judged according to comparative result.
With reference to second aspect, in second possible implementation of second aspect, described according to the identifier lookup
Data after compression are decompressed by corresponding compression algorithm according to the compression algorithm, the data step for being decompressed
Before, methods described also includes:
The length of the data after the compression stored in the 3rd specified location in packet header is obtained, and in the compressed data packets
Compression after data length, whether length according to the data after compression judge data with the comparative result of the length of storage
Completely;
And described according to the corresponding compression algorithm of the identifier lookup, according to the compression algorithm to the number after compression
According to being decompressed, after the data step for being decompressed, methods described also includes:
Obtain packet header the 4th specified location storage compression before data length, and decompression data length
Degree, length and the comparative result of the length of storage of the data according to decompression judge whether the data after decompression are complete.
The third aspect, the embodiment of the invention provides a kind of data compression device, and described device includes:
Request unit, for receiving data compression request, obtains selected by the data and the compression data for needing compression
The compression algorithm selected;
Compression unit, for being compressed to the data for needing compression according to selected compression algorithm, after being compressed
Data;
Compressed data packets generation unit, the first specific bit for the corresponding mark of the compression algorithm to be added to packet header
Put, by the data after the compression and packet header generation compressed data packets.
With reference to the third aspect, in the first possible implementation of the third aspect, described device also includes:
Verification unit, for being circulated redundancy check to the data for needing compression, generates CRC result;
Check results adding device, the second specific bit for the CRC result to be added to the packet header
Put.
With reference to the third aspect, in second possible implementation of the third aspect, described device also includes:
Original length acquiring unit, for the length for obtaining the data after compression and the original length of the data for needing compression
Degree;
Original length writing unit, the 3rd specific bit for the length of the data after the compression to be write the packet header
Put, and the 4th specified location in the original length write-in packet header of the data of compression will be needed.
Fourth aspect, the embodiment of the invention provides a kind of data decompressor, and described device includes:
Data capture unit, for receiving data decompression request, obtains the institute of the data after the compression in compressed data packets right
The corresponding mark of compression algorithm answered, and the data after the specified location of compressed data packets obtains compression;
Decompression units, for according to the corresponding compression algorithm of the identifier lookup, according to the compression algorithm to compression after
Data decompressed, the data for being decompressed.
In the present invention, when being compressed to data, the data for needing compression are carried out by the compression algorithm for selecting
Compression, and will select compression algorithm mark addition packet header the first specified location, by packet header and compress after data into
Raw compressed data packets.When being decompressed, the mark of the compression algorithm of first specified location in the packet header is read, call correspondence
Compression algorithm to compress data decompress, the data for being parsed.By the setting of the mark of compression algorithm, can make
This method can adapt to different algorithms compressed file decompression and compression, be conducive to improving the convenience that uses of user.
Brief description of the drawings
Fig. 1 is that the data compression method that first embodiment of the invention is provided realizes flow chart;
Fig. 2 is that the data decompression method that second embodiment of the invention is provided realizes flow chart;
Fig. 3 is the structural representation of the data compression device that third embodiment of the invention is provided;
The structural representation of the data decompressor that Fig. 4 is provided for third embodiment of the invention.
Specific embodiment
In order to make the purpose , technical scheme and advantage of the present invention be clearer, it is right below in conjunction with drawings and Examples
The present invention is further elaborated.It should be appreciated that the specific embodiments described herein are merely illustrative of the present invention, and
It is not used in the restriction present invention.
The purpose of the embodiment of the present invention is to provide a kind of data compression method and device, with solve in the prior art due to
Tool of compression is various, and the compression algorithm that different tools of compression is used would also vary from, and cause different works
Cannot be to the identification of data packets of compression and decompression between tool.Such as, when the instrument X using the first compression algorithm carries out compressing file
Afterwards, if carrying out that when file is decompressed failure may be decompressed using the instrument Y of the second compression algorithm.Thus resulting in user needs
Terminal installs multiple tools of compression, and operation is more bothered.
Embodiment one:
Fig. 1 shows that the data compression method that first embodiment of the invention is provided realizes flow, and details are as follows:
In step S101, data compression request is received, obtain the data of needs compression and compress selected by the data
The compression algorithm selected.
Specifically, it is described reception data compression request, can for user need to be compressed data when, call this method
In data compression program interface, generation data compression request.
The data for needing to compress, that is, the selected file for needing to compress of user.The file can be sound
Frequency file, video file, text or other files etc..The data for needing to compress, can be a file, also may be used
Think the combination of multiple files.
The selected compression algorithm of compressed data, can be after the data for obtaining needing compression, to number to be compressed
Test analysis and judgement are originally compressed according to sampling.Such as, the data for judging to need compression are video file, or judge to need
The data of compression are audio file, or judge that it is text etc. to need the data compressed, and the sample data according to file is surveyed
Examination compression result, it is good to search that compression algorithm compression effectiveness, just selects that compression algorithm.It is, of course, also possible to according to user
Selection request, compression algorithm is selected by user.
The compression algorithm can be previously integrated in the tool of compression corresponding to this method, or can be by compression algorithm
It is integrated in the server, after tool of compression have selected corresponding compression algorithm, obtain corresponding in local or server
Compression algorithm.
In step s 102, the data for needing compression are compressed according to selected compression algorithm, after being compressed
Data.
After have selected compression algorithm in step S101, according to the compression algorithm of selection, the data to needing compression are carried out
Compression, the data after being compressed.The memory space shared by data after the compression is less than the data for needing and compressing
Shared memory space.
In step s 103, the corresponding mark of the compression algorithm is added to first specified location in packet header, by described
Data and packet header generation compressed data packets after compression.
According to the compression algorithm of selection, the corresponding mark of the compression algorithm can be found.Compression algorithm can be set
With the corresponding relation of mark, the type of compression algorithm is led into first specified location of too small amount of byte records in packet header.Such as,
First specified location can be a small amount of byte, can be 4 bytes.
The data of the compression and packet header are into after raw compressed data packets, you can the packet after compression is preserved or
Transmission, can carry out decompression operations, or carried out in miscellaneous equipment for the instrument corresponding to this method for other tools of compression
Decompression operations, the data after being decompressed, that is, the data before compressing.
Certainly, in as the further implementation method of optimization of the invention, in the reception data compression request, obtaining needs
After the step of data of compression and the selection compression data selected compression algorithm, methods described also includes:To needing
The data to be compressed are circulated redundancy check, generate CRC result;By CRC result addition
To second specified location in the packet header.
The packet header can set the second specified location, and CRC result is stored in the second specified location.
Wherein, the CRC result is generated to needing the data of compression to be circulated redundancy check.Followed by setting
Data after decompression, when being decompressed to compressed package, can be circulated redundancy check, so as to test by ring redundancy check result
Whether the data after card compression are complete.If verification error, illustrates in compression or decompression procedure, there is loss of data or go out
It is wrong.
In addition, in further optimal way, in the reception data compression request, obtain the data that need compression and
After the step of selection compression data selected compression algorithm, methods described also includes:
The original length of the length for obtaining the data after compression and the data for needing compression;By the data after the compression
Length write the 3rd specified location in packet header, and the 4th that the original length write-in packet header of the data of compression will be needed
Specified location.
The length of the data after compression is set in packet header, and need the original length of the data of compression.Work as user
When being decompressed to compressed package, can first according to the length of the data after the compression stored in packet header, after judging current compression
Whether the length stored with the 3rd specified location is consistent the length of data.Follow-up decompression operations are performed if being consistent, and
Judge whether the length of the data after decompression is consistent with the length of the 4th specified location storage, can be sent if not corresponding and reported an error
Remind.
Data compression method of the present invention, the compression algorithm required for can selecting is pressed the data for needing compression
Contracting, thus the compressed package corresponding to different compression algorithms can be generated.It is adapted to the requirement of different decompression tools.And use
Decompressing method corresponding with this method, as shown in Figure 2, packet that can be directly to algorithms of different compression is decompressed, greatly
Big improves the convenience that user uses.
Embodiment two:
Fig. 2 shows that the data decompression method that second embodiment of the invention is provided realizes flow, and details are as follows:
In step s 201, data decompression request is received, is obtained corresponding to the data after the compression in compressed data packets
The corresponding mark of compression algorithm, and the data after the specified location of compressed data packets obtains compression.
Corresponding with the compression method in embodiment one, decompressing method of the present invention receives data decompression request, can be with
By calling the data decompression routine interface in this method, decompression request is initiated, or by opening the corresponding work of this method
Tool, in the tool triggering decompression instruction.
When being decompressed to compressed package, the mark of the compression algorithm in the packet header of compressed data packets, Yi Jisuo are obtained first
The specified location for stating compressed data packets obtains the data after compression.Data after the compression can be stored in the compressed data
Behind the packet header of bag.
In step S202, according to the corresponding compression algorithm of the identifier lookup, according to the compression algorithm to compression after
Data decompressed, the data for being decompressed.
According to the corresponding mark of the compression algorithm, corresponding compression algorithm can be found.According to the compression for finding
Data after compression in compressed data packets can be carried out decompression operations by algorithm, be adapted to corresponding to different compression algorithms
Compression after data solution pressure request.
Certainly, the compression algorithm can be stored in the instrument corresponding to this method, it is also possible to stored in the server,
After corresponding compression algorithm is found, can be from locally obtaining corresponding compression algorithm, it is also possible to obtain correspondence from server
Compression algorithm.
In addition, as the present invention further optimization, described according to the corresponding compression algorithm of the identifier lookup, according to institute
State compression algorithm to decompress the data after compression, after the data step for being decompressed, methods described also includes:
The data for decompressing are verified by circulating result method of calibration, the check results of generation decompression data;By institute
State check results of the check results of decompression data with storage in the specified location of packet header second to be compared, sentenced according to comparative result
The integrality of the data of the disconnected decompression.
Cyclic check result is set by packet header, the data after decompression can be verified, after finding decompression in time
Data whether had modification or loss of data.
In addition, it is corresponding with optimal way in embodiment one, described according to the corresponding compression algorithm of the identifier lookup,
The data after compression are decompressed according to the compression algorithm, before the data step for being decompressed, methods described is also
Including:
The length of the data after the compression stored in the 3rd specified location in packet header is obtained, and in the compressed data packets
Compression after data length, whether length according to the data after compression judge data with the comparative result of the length of storage
Completely;
And described according to the corresponding compression algorithm of the identifier lookup, according to the compression algorithm to the number after compression
According to being decompressed, after the data step for being decompressed, methods described also includes:
Obtain packet header the 4th specified location storage compression before data length, and decompression data length
Degree, length and the comparative result of the length of storage of the data according to decompression judge whether the data after decompression are complete.
Mark corresponding to compression algorithm is searched by the first specified location in packet header, and is calculated according to the compression searched
Data after compression are decompressed by method, thus this method is adapted to the decompression of the data after different tools of compression compress
It is required that.Be conducive to improving the convenience that user uses.And compressed data packets are generated by compression method corresponding with this method,
The demand that user uses different compression algorithms to data can be greatly improved.
Embodiment three:
Fig. 3 shows the structural representation of the data compression device that third embodiment of the invention is provided, and details are as follows:
Data compression device described in the embodiment of the present invention, including:
Request unit 301, for receiving data compression request, obtains the data and the compression data institute for needing compression
The compression algorithm of selection;
Compression unit 302, for being compressed to the data for needing compression according to selected compression algorithm, is compressed
Data afterwards;
Compressed data packets generation unit 303, the first finger for the corresponding mark of the compression algorithm to be added to packet header
Positioning is put, by the data after the compression and packet header generation compressed data packets.
Preferably, described device also includes:
Verification unit, for being circulated redundancy check to the data for needing compression, generates CRC result;
Check results adding device, the second specific bit for the CRC result to be added to the packet header
Put.
Preferably, described device also includes:
Original length acquiring unit, for the length for obtaining the data after compression and the original length of the data for needing compression
Degree;
Original length writing unit, the 3rd specific bit for the length of the data after the compression to be write the packet header
Put, and the 4th specified location in the original length write-in packet header of the data of compression will be needed.
Data compression device described in Fig. 3, the description that is herein not repeated corresponding with data compression method described in Fig. 1.
In addition, present invention also offers a kind of data decompressor, as shown in figure 4, the data decompressor, including:
Data capture unit 401, for receiving data decompression request, obtains the data institute after the compression in compressed data packets
The corresponding mark of corresponding compression algorithm, and the data after the specified location of compressed data packets obtains compression;
Decompression units 402, for according to the corresponding compression algorithm of the identifier lookup, according to the compression algorithm to compression
Data afterwards are decompressed, the data for being decompressed.
Preferably, described device also includes:
Check results generation unit, for being verified to the data for decompressing by circulating result method of calibration, generation solution
Press the check results of data;
Check results comparing unit, for by it is described decompression data check results with storage in the specified location of packet header second
Check results be compared, the integrality of the data of the decompression is judged according to comparative result.
Preferably, described device also includes:
First length comparing unit, for the length of the data after the compression of storage in the 3rd specified location for obtaining packet header
The length of the data after degree, and compression in the compressed data packets, the length of length according to the data after compression and storage
The comparative result of degree judges whether data are complete;
Second length comparing unit, the length of the data before the compression that the 4th specified location for obtaining packet header is stored,
And the length of the data of decompression, after the length of the data according to decompression judges decompression with the comparative result of the length of storage
Data it is whether complete.
Data compression device described in the embodiment of the present invention, data decompressor, respectively with data described in embodiment one
Data decompression method correspondence in compression method, embodiment two.
In several embodiments provided by the present invention, it should be understood that disclosed apparatus and method, can be by it
Its mode is realized.For example, device embodiment described above is only schematical, for example, the division of the unit, only
Only a kind of division of logic function, can there is other dividing mode when actually realizing, such as multiple units or component can be tied
Another system is closed or is desirably integrated into, or some features can be ignored, or do not perform.It is another, it is shown or discussed
Coupling each other or direct-coupling or communication connection can be the INDIRECT COUPLINGs or logical of device or unit by some interfaces
Letter connection, can be electrical, mechanical or other forms.
The unit that is illustrated as separating component can be or may not be it is physically separate, it is aobvious as unit
The part for showing can be or may not be physical location, you can with positioned at a place, or can also be distributed to multiple
On NE.Some or all of unit therein can be according to the actual needs selected to realize the mesh of this embodiment scheme
's.
In addition, during each functional unit in each embodiment of the invention can be integrated in a processing unit, it is also possible to
It is that unit is individually physically present, it is also possible to which two or more units are integrated in a unit.Above-mentioned integrated list
Unit can both be realized in the form of hardware, it would however also be possible to employ the form of SFU software functional unit is realized.
If the integrated unit is to realize in the form of SFU software functional unit and as independent production marketing or use
When, can store in a computer read/write memory medium.Based on such understanding, technical scheme is substantially
The part for being contributed to prior art in other words or all or part of the technical scheme can be in the form of software products
Embody, the computer software product is stored in a storage medium, including some instructions are used to so that a computer
Equipment (can be personal computer, server, or network equipment etc.) performs the complete of each embodiment methods described of the invention
Portion or part.And foregoing storage medium includes:USB flash disk, mobile hard disk, read-only storage (ROM, Read-Only Memory),
Random access memory (RAM, Random Access Memory), magnetic disc or CD etc. are various can be with store program codes
Medium.
Presently preferred embodiments of the present invention is the foregoing is only, is not intended to limit the invention, it is all in essence of the invention
Any modification, equivalent and improvement made within god and principle etc., should be included within the scope of the present invention.