Summary of the invention
In order to solve the problems of the technologies described above, the technical solution adopted in the present invention is: a kind of file packing method based on Hash, and packaging method may further comprise the steps:
A, read the file of required packing successively;
The cryptographic hash of B, the above-mentioned file path of calculating;
C, above-mentioned file is write file data district in the APMB package;
D, the off-set value of the cryptographic hash of file path and file is write Hash table in the APMB package;
E, judge whether to have read the file that all need be packed, if not, then continue execution in step A, if, the then packing of ends file.
Further, step B comprises following substep:
B1, calculation document path cryptographic hash;
B2, judge whether hash-collision;
If there is not hash-collision in B3, then continue execution in step C; If there is hash-collision, then change hash algorithm or file path title after, get back to step B1 and re-execute.
Further, do not preserve routing information, bibliographic structure or the filename of package-in file in the APMB package.
In order to solve the problems of the technologies described above, another technical scheme of the present invention is: a kind of file reading based on Hash, and read method may further comprise the steps:
F, the instrument that reads receive the index information of the required file that reads from upper layer application, the cryptographic hash in the path by file index acquisition of information package-in file;
G, in Hash table, search off-set value corresponding to cryptographic hash;
H, the file data district in APMB package read the data file corresponding with off-set value.
Further, the file index information in the step F is the path of package-in file.
Further, the file index information in the step F is the cryptographic hash in the path of package-in file.
The invention has the beneficial effects as follows: adopt packaging method of the present invention, each file in the cryptographic hash unique identification bag in the APMB package, Hash table is preserved the corresponding relation of cryptographic hash and off-set value, read package-in file and do not need to carry out the word string coupling, only can from Hash table, locate fast the offset information of concrete package-in file by the cryptographic hash of identification document, improve greatly reading speed.
Another beneficial effect of the present invention is: adopt read method of the present invention, compare with reading manner in the past, reading in the process not to need to use file path, thereby malicious user can't restore by the file after the packing the front original directory structure of packing.
Embodiment
Below in conjunction with accompanying drawing the specific embodiment of the present invention is described further:
A kind of file packing and read method based on Hash namely is by packing process and read packet procedures and then finish writing and read operation package-in file.
With reference to the APMB package among Fig. 1, packing process is about to file and is written to process in the APMB package, and it is finished by strapping tool.Strapping tool receives following input message:
1, the file directory of required packing or listed files;
2, employed hash algorithm;
3, the way of output and outgoing position.
With reference to Fig. 2, a kind of file packing method based on Hash, described packaging method may further comprise the steps:
A, read the file of required packing successively;
Strapping tool reads corresponding file according to input message from disk, and then generates corresponding file bag.The file bag include file data field that generates and two parts of Hash table.The file data district provides the place for the package-in file storage, and wherein each package-in file is called the off-set value of this package-in file in the position in file data district.
The cryptographic hash of B, the above-mentioned file path of calculating;
Preserve the corresponding relation of each cryptographic hash and off-set value in the Hash table, be used for reading packet procedures file is carried out fast finding.
C, file is write file data district in the APMB package.
D, the off-set value of the cryptographic hash of file path and file is write Hash table in the APMB package;
When All Files when all writing in files data field and all cryptographic hash are identical, then this moment is with the key of cryptographic hash as Hash table.
E, judge whether to have read the file that all need be packed, if not, then continue execution in step A, if, the then packing of ends file.
If read the file that all need be packed, then the file packing process finishes, and obtains file bag as shown in Figure 1.
With reference to Fig. 3, strapping tool reads the file of needs packing according to file directory or listed files, and the file that reads is written in the file data district, and records at this moment the position of file in the data field, and this position i.e. the off-set value of file for this reason.Use afterwards the hash algorithm of appointment, for file path calculates cryptographic hash.
Be further used as preferred embodiment, described step B comprises following substep:
B1, calculation document path cryptographic hash;
B2, judge whether hash-collision;
If there is not hash-collision in B3, then continue execution in step C; If there is hash-collision, then change hash algorithm or file path title after, get back to step B1 and re-execute.
If this cryptographic hash is not identical with all cryptographic hash that All Files before calculated, then can be with the sign of this value as file.If any cryptographic hash of calculating of preceding document is identical with it for this cryptographic hash, then require to change hash algorithm or revise packaged file path.Otherwise packing process can't continue.
Be further used as preferred embodiment, do not preserve routing information, bibliographic structure or the filename of package-in file in the described APMB package.
With reference to Fig. 4, a kind of file reading based on Hash is read packet procedures and is finished by corresponding client-side program, and described read method may further comprise the steps:
F, the instrument that reads receive the index information of the required file that reads from upper layer application, the cryptographic hash in the path by file index acquisition of information package-in file;
G, in Hash table, search off-set value corresponding to cryptographic hash;
H, the file data district in APMB package read data file corresponding to off-set value.
Its reading manner to package-in file is divided into two kinds:
1. read by file path;
2. the cryptographic hash by file path reads.Two kinds of file index information that APMB package reads are specified by client configuration item or parameter.
Be further used as preferred embodiment, the file index information in the described step F is the path of package-in file, and the cryptographic hash in the path of described package-in file calculates by corresponding hash algorithm.Be the required file path that reads that client imports into, and use hash algorithm identical when packing to calculate the cryptographic hash of this file path.Through after calculating cryptographic hash, can in Hash table, find out the off-set value of this package-in file.Read the file of off-set value position, namely finish file and read.
Be further used as preferred embodiment, the file index information in the described step F is the cryptographic hash in the path of package-in file.Adopt the cryptographic hash of file path to read, then client imports the required file path cryptographic hash that reads into.After obtaining this cryptographic hash, directly in Hash table, search, get final product to get the off-set value of this package-in file.Read the file of off-set value position, namely finish file and read.
More than be that better enforcement of the present invention is specified, but the invention is not limited to described embodiment, those of ordinary skill in the art can also make all equivalents or replacement under the prerequisite of spirit of the present invention, the conversion that these are equal to or replacement all are included in the application's claim limited range.