Data compression method and device
Technical Field
The invention belongs to the field of data compression, and particularly relates to a data compression method and device.
Background
Data compression is a method for reorganizing data according to a certain algorithm on the premise of not losing useful information, reducing the redundancy and storage space of the data and improving the transmission, storage and processing efficiency of the data. After the data to be stored and transmitted is compressed by the data compression algorithm, a smaller storage space can be used for storing more data, and in the data transmission process, network resources can be greatly saved and the transmission efficiency is improved.
At present, when data is compressed, existing data compression tools, such as WINRAR, WINZIP, good compression, 360 compression and the like, are generally selected. And the compression algorithms employed by different compression tools may also be different. Therefore, when a user uses the compression tool a for compression, in different use environments, such as sending compressed data to other users or copying the compressed data to other devices, the data may not be effectively decompressed due to the difference of the compression tools. The user is required to install a corresponding compression tool in the new device, and the operation is troublesome.
Disclosure of Invention
The invention aims to provide a data compression method and a data compression device, which are used for solving the problems that compressed data cannot be effectively adapted to different tools for decompression and the operation of a user is inconvenient in the prior art due to various compression tools.
In a first aspect, an embodiment of the present invention provides a data compression method, where the method includes:
receiving a data compression request, acquiring data to be compressed and a compression algorithm selected by compressing the data;
compressing the data to be compressed according to the selected compression algorithm to obtain compressed data;
and adding the identifier corresponding to the compression algorithm to a first specified position of the packet header, and generating a compressed data packet by the compressed data and the packet header.
With reference to the first aspect, in a first possible implementation manner of the first aspect, after the steps of receiving a data compression request, acquiring data to be compressed, and selecting a compression algorithm selected for compressing the data, the method further includes:
performing cyclic redundancy check on data to be compressed to generate a cyclic redundancy check result;
and adding the cyclic redundancy check result to a second appointed position of the packet header.
With reference to the first aspect, in a second possible implementation manner of the first aspect, after the steps of receiving a data compression request, acquiring data to be compressed, and selecting a compression algorithm selected for compressing the data, the method further includes:
acquiring the length of the compressed data and the original length of the data needing to be compressed;
writing the length of the compressed data into a third designated position of the packet header, and writing the original length of the data needing to be compressed into a fourth designated position of the packet header.
In a second aspect, an embodiment of the present invention provides a data decompression method, where the method includes:
receiving a data decompression request, acquiring an identifier corresponding to a compression algorithm corresponding to compressed data in a compressed data packet, and acquiring the compressed data at a specified position of the compressed data packet;
and searching a corresponding compression algorithm according to the identifier, and decompressing the compressed data according to the compression algorithm to obtain decompressed data.
With reference to the second aspect, in a first possible implementation manner of the second aspect, after the step of searching for a corresponding compression algorithm according to the identifier, and decompressing the compressed data according to the compression algorithm to obtain decompressed data, the method further includes:
checking the decompressed data by a cyclic result checking method to generate a checking result of the decompressed data;
and comparing the verification result of the decompressed data with the verification result stored at the second appointed position of the packet header, and judging the integrity of the decompressed data according to the comparison result.
With reference to the second aspect, in a second possible implementation manner of the second aspect, before the step of searching for a corresponding compression algorithm according to the identifier, and decompressing the compressed data according to the compression algorithm to obtain decompressed data, the method further includes:
acquiring the length of compressed data stored in a third appointed position of the packet header and the length of the compressed data in the compressed data packet, and judging whether the data is complete according to the comparison result of the length of the compressed data and the stored length;
after the step of searching for the corresponding compression algorithm according to the identifier and decompressing the compressed data according to the compression algorithm to obtain decompressed data, the method further comprises:
and acquiring the length of the data before compression stored in the fourth specified position of the packet header and the length of the decompressed data, and judging whether the decompressed data is complete or not according to the comparison result of the length of the decompressed data and the stored length.
In a third aspect, an embodiment of the present invention provides a data compression apparatus, where the apparatus includes:
the request unit is used for receiving a data compression request, acquiring data to be compressed and a compression algorithm selected by compressing the data;
the compression unit is used for compressing the data to be compressed according to the selected compression algorithm to obtain compressed data;
and the compressed data packet generating unit is used for adding the identifier corresponding to the compression algorithm to a first specified position of the packet header and generating a compressed data packet by the compressed data and the packet header.
With reference to the third aspect, in a first possible implementation manner of the third aspect, the apparatus further includes:
the check unit is used for performing cyclic redundancy check on the data to be compressed to generate a cyclic redundancy check result;
and the check result adding unit is used for adding the cyclic redundancy check result to a second appointed position of the packet header.
With reference to the third aspect, in a second possible implementation manner of the third aspect, the apparatus further includes:
an original length obtaining unit, configured to obtain a length of the compressed data and an original length of the data to be compressed;
and the original length writing unit is used for writing the length of the compressed data into a third specified position of the packet header and writing the original length of the data needing to be compressed into a fourth specified position of the packet header.
In a fourth aspect, an embodiment of the present invention provides a data decompression apparatus, where the apparatus includes:
the data acquisition unit is used for receiving a data decompression request, acquiring an identifier corresponding to a compression algorithm corresponding to compressed data in a compressed data packet, and acquiring the compressed data at a specified position of the compressed data packet;
and the decompression unit is used for searching a corresponding compression algorithm according to the identifier, decompressing the compressed data according to the compression algorithm and obtaining decompressed data.
In the invention, when data is compressed, the data to be compressed is compressed by a selected compression algorithm, the identifier of the selected compression algorithm is added to the first specified position of the packet header, and a compressed data packet is generated by the packet header and the compressed data. When decompression is carried out, the identification of the compression algorithm at the first designated position of the packet header is read, and the corresponding compression algorithm is called to decompress the compressed data to obtain the analyzed data. By setting the identification of the compression algorithm, the method can adapt to the decompression and compression of the compressed files with different algorithms, and is beneficial to improving the use convenience of users.
Drawings
Fig. 1 is a flowchart of an implementation of a data compression method according to a first embodiment of the present invention;
FIG. 2 is a flowchart of an implementation of a data decompression method according to a second embodiment of the present invention;
FIG. 3 is a schematic structural diagram of a data compression apparatus according to a third embodiment of the present invention;
fig. 4 is a schematic structural diagram of a data decompression device according to a third embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
The embodiments of the present invention provide a data compression method and apparatus, so as to solve the problem in the prior art that compressed data packets cannot be identified and decompressed between different tools because various compression tools are used and compression algorithms adopted by different compression tools are different. For example, after a file is compressed using a first compression algorithm tool X, the file may fail to be decompressed if a second compression algorithm tool Y is used to decompress the file. Therefore, the user needs to install a plurality of compression tools at the terminal, and the operation is troublesome.
The first embodiment is as follows:
fig. 1 shows an implementation flow of a data compression method provided by a first embodiment of the present invention, which is detailed as follows:
in step S101, a data compression request is received, and data to be compressed and a compression algorithm selected for compressing the data are acquired.
Specifically, the receiving of the data compression request may call a data compression program interface in the method to generate the data compression request when the user needs to compress the data.
The data needing to be compressed is the file needing to be compressed and selected by the user. The file may be an audio file, a video file, a text file, or other file, etc. The data to be compressed may be one file or a combination of multiple files.
The compression algorithm selected by the compressed data can be used for performing compression test analysis and judgment on a sample of the data to be compressed after the data to be compressed is obtained. For example, the data to be compressed is judged to be a video file, or the data to be compressed is judged to be an audio file, or the data to be compressed is judged to be a text file, and the like, and the compression result is tested according to the sample data of the file, so that the compression effect of the compression algorithm is good, and the compression algorithm is selected. Of course, the compression algorithm may also be selected by the user based on a selection request from the user.
The compression algorithm may be integrated in the compression tool corresponding to the method in advance, or the compression algorithm may be integrated in the server, and after the compression tool selects the corresponding compression algorithm, the corresponding compression algorithm is acquired locally or in the server.
In step S102, the data to be compressed is compressed according to the selected compression algorithm, so as to obtain compressed data.
After the compression algorithm is selected in step S101, the data to be compressed is compressed according to the selected compression algorithm, and compressed data is obtained. And the storage space occupied by the compressed data is smaller than that occupied by the data needing to be compressed.
In step S103, the identifier corresponding to the compression algorithm is added to the first designated position of the packet header, and a compressed data packet is generated from the compressed data and the packet header.
According to the selected compression algorithm, the identifier corresponding to the compression algorithm can be found. The corresponding relation between the compression algorithm and the identifier can be set, and the type of the compression algorithm is recorded in a first designated position of the packet header through a small number of bytes. For example, the first designated location may be a small number of bytes, and may be 4 bytes.
After the compressed data and the packet header generate a compressed data packet, the compressed data packet can be stored or transmitted, and can be used for decompression by other compression tools, or used for decompression by a tool corresponding to the method in other devices, so that decompressed data, namely data before compression, is obtained.
Of course, in a further optimized embodiment of the present invention, after the steps of receiving a data compression request, obtaining data to be compressed, and selecting a compression algorithm selected for compressing the data, the method further includes: performing cyclic redundancy check on data to be compressed to generate a cyclic redundancy check result; and adding the cyclic redundancy check result to a second appointed position of the packet header.
The header may be provided with a second designated location, and the cyclic redundancy check result may be stored in the second designated location. And the cyclic redundancy check result is generated by performing cyclic redundancy check on the data needing to be compressed. By setting the cyclic redundancy check result, when the compressed packet is decompressed, cyclic redundancy check can be performed on the decompressed data, so that whether the compressed data is complete or not can be verified. If the check is wrong, the data loss or the error exists in the compression or decompression process.
In addition, in a further optimization mode, after the steps of receiving a data compression request, acquiring data to be compressed, and selecting a compression algorithm selected for compressing the data, the method further includes:
acquiring the length of the compressed data and the original length of the data needing to be compressed; writing the length of the compressed data into a third designated position of the packet header, and writing the original length of the data needing to be compressed into a fourth designated position of the packet header.
The length of the compressed data and the original length of the data needing to be compressed are set in the packet header. When the user decompresses the compressed packet, it may be determined whether the length of the current compressed data matches the length stored at the third designated location according to the length of the compressed data stored in the packet header. And if the length of the decompressed data is not consistent with the length stored in the fourth designated position, an error-reporting prompt is sent out.
The data compression method can select the required compression algorithm to compress the data to be compressed, thereby generating the compression packets corresponding to different compression algorithms. Can meet the requirements of different decompression tools. And the decompression method corresponding to the method, such as the decompression method shown in fig. 2, can directly decompress the data packets compressed by different algorithms, thereby greatly improving the convenience of use for users.
Example two:
fig. 2 shows an implementation flow of a data decompression method provided by a second embodiment of the present invention, which is detailed as follows:
in step S201, a data decompression request is received, an identifier corresponding to a compression algorithm corresponding to compressed data in a compressed data packet is obtained, and the compressed data is obtained at a specified position of the compressed data packet.
Corresponding to the compression method in the first embodiment, the decompression method receives the data decompression request, and may initiate the decompression request by calling a data decompression program interface in the method, or trigger the decompression instruction in a tool by opening a tool corresponding to the method.
When a compressed packet is decompressed, firstly, an identifier of a compression algorithm of a packet header of the compressed data packet is obtained, and compressed data is obtained at a specified position of the compressed data packet. The compressed data may be stored behind the header of the compressed data packet.
In step S202, a corresponding compression algorithm is searched according to the identifier, and the compressed data is decompressed according to the compression algorithm to obtain decompressed data.
And according to the identifier corresponding to the compression algorithm, the corresponding compression algorithm can be found. According to the searched compression algorithm, decompression operation can be carried out on the compressed data in the compressed data packet, and the decompression requirements of the compressed data corresponding to different compression algorithms can be met.
Of course, the compression algorithm may be stored in the tool corresponding to the method, or may be stored in the server, and after the corresponding compression algorithm is found, the corresponding compression algorithm may be obtained locally, or may be obtained from the server.
In addition, as a further optimization of the present invention, after the steps of searching for a corresponding compression algorithm according to the identifier, and decompressing the compressed data according to the compression algorithm to obtain decompressed data, the method further includes:
checking the decompressed data by a cyclic result checking method to generate a checking result of the decompressed data; and comparing the verification result of the decompressed data with the verification result stored at the second appointed position of the packet header, and judging the integrity of the decompressed data according to the comparison result.
By setting a cyclic check result in the packet header, the decompressed data can be checked, and whether the decompressed data is modified or lost can be found in time.
In addition, corresponding to the optimization manner in the first embodiment, before the steps of searching for the corresponding compression algorithm according to the identifier, and decompressing the compressed data according to the compression algorithm to obtain decompressed data, the method further includes:
acquiring the length of compressed data stored in a third appointed position of the packet header and the length of the compressed data in the compressed data packet, and judging whether the data is complete according to the comparison result of the length of the compressed data and the stored length;
after the step of searching for the corresponding compression algorithm according to the identifier and decompressing the compressed data according to the compression algorithm to obtain decompressed data, the method further comprises:
and acquiring the length of the data before compression stored in the fourth specified position of the packet header and the length of the decompressed data, and judging whether the decompressed data is complete or not according to the comparison result of the length of the decompressed data and the stored length.
The identifier corresponding to the compression algorithm is searched for in the first designated position of the packet header, and the compressed data is decompressed according to the searched compression algorithm, so that the method can adapt to the decompression requirements of the data compressed by different compression tools. Is beneficial to improving the convenience of the user. And the compressed data packet is generated by the compression method corresponding to the method, so that the requirements of users on adopting different compression algorithms for data can be greatly improved.
Example three:
fig. 3 shows a schematic structural diagram of a data compression apparatus according to a third embodiment of the present invention, which is detailed as follows:
the data compression device of the embodiment of the invention comprises:
a request unit 301, configured to receive a data compression request, obtain data to be compressed, and compress a compression algorithm selected by the data;
a compressing unit 302, configured to compress data to be compressed according to the selected compression algorithm to obtain compressed data;
a compressed data packet generating unit 303, configured to add an identifier corresponding to the compression algorithm to a first specified location of the packet header, and generate a compressed data packet from the compressed data and the packet header.
Preferably, the apparatus further comprises:
the check unit is used for performing cyclic redundancy check on the data to be compressed to generate a cyclic redundancy check result;
and the check result adding unit is used for adding the cyclic redundancy check result to a second appointed position of the packet header.
Preferably, the apparatus further comprises:
an original length obtaining unit, configured to obtain a length of the compressed data and an original length of the data to be compressed;
and the original length writing unit is used for writing the length of the compressed data into a third specified position of the packet header and writing the original length of the data needing to be compressed into a fourth specified position of the packet header.
The data compression apparatus shown in fig. 3 corresponds to the data compression method shown in fig. 1, and will not be described repeatedly herein.
In addition, the present invention also provides a data decompression apparatus, as shown in fig. 4, the data decompression apparatus including:
a data obtaining unit 401, configured to receive a data decompression request, obtain an identifier corresponding to a compression algorithm corresponding to compressed data in a compressed data packet, and obtain the compressed data at a specified position of the compressed data packet;
a decompression unit 402, configured to search for a corresponding compression algorithm according to the identifier, and decompress the compressed data according to the compression algorithm to obtain decompressed data.
Preferably, the apparatus further comprises:
the verification result generating unit is used for verifying the decompressed data by a cyclic result verification method to generate a verification result of the decompressed data;
and the verification result comparison unit is used for comparing the verification result of the decompressed data with the verification result stored at the second appointed position of the packet header and judging the integrity of the decompressed data according to the comparison result.
Preferably, the apparatus further comprises:
a first length comparing unit, configured to obtain the length of the compressed data stored in the third specified location of the packet header and the length of the compressed data in the compressed data packet, and determine whether the data is complete according to a comparison result between the length of the compressed data and the stored length;
and the second length comparison unit is used for acquiring the length of the data before compression stored in the fourth specified position of the packet header and the length of the decompressed data, and judging whether the decompressed data is complete according to the comparison result of the length of the decompressed data and the stored length.
The data compression device and the data decompression device in the embodiment of the present invention correspond to the data compression method in the first embodiment and the data decompression method in the second embodiment, respectively.
In the embodiments provided in the present invention, it should be understood that the disclosed apparatus and method may be implemented in other ways. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the units is only one logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing program codes.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents and improvements made within the spirit and principle of the present invention are intended to be included within the scope of the present invention.