File comparison method and device based on traversal algorithm, and electronic device
By using a traversal algorithm to automatically compare file packages, generate a set of file information and compare differences, the problem of complex and large-scale file comparison that cannot be handled manually in existing technologies is solved, and efficient and accurate file difference analysis is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- INDUSTRIAL AND COMMERCIAL BANK OF CHINA
- Filing Date
- 2023-03-14
- Publication Date
- 2026-07-21
AI Technical Summary
Existing methods for manual document comparison are not suitable for comparing complex and large-scale documents, as they are inefficient and have low accuracy.
A traversal algorithm is used to traverse the file packages to be compared, generate a set of file information, and obtain the difference information by comparing the set of file information to automatically perform file comparison.
It enables rapid comparison of complex and large batches of files, improving comparison efficiency and outputting clear and intuitive difference information without manual operation.
Smart Images

Figure CN116483781B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of financial technology and other related technical fields. Specifically, it relates to a file comparison method, apparatus, and electronic device based on a traversal algorithm. Background Technology
[0002] When transferring and using files, differences may exist between the existing files and the original files due to file version updates or uncertainties in the transfer process.
[0003] In related technologies, the comparison of folders or text is generally based on binary data, specific rules, etc. For traditional compressed files, such as zip and jar, the internal files can also be compared during the decompression process, and rules can be added to filter the comparison of specific files. However, when comparing folders using existing comparison tools, it is necessary to manually locate the folder to be compared. The file comparison can only be achieved through manual operation, which is only suitable for comparing a small number of files and not suitable for comparing complex and large-scale files.
[0004] There is currently no effective solution to the above problems. Summary of the Invention
[0005] This invention provides a file comparison method, apparatus, and electronic device based on a traversal algorithm, which at least solves the technical problem that manual file comparison methods in related technologies are not suitable for comparing complex and large-scale files.
[0006] According to one aspect of the present invention, a file comparison method based on a traversal algorithm is provided, comprising: obtaining file paths of a first file package to be compared and a second file package to be compared, wherein the first file package to be compared is a file package to be checked, and the second file package to be compared is a reference file package corresponding to the first file package to be compared; traversing the first file package to be compared and the second file package to be compared under the file paths using a traversal algorithm to generate a first file information set for each file in the first file package to be compared and a second file information set for each file in the second file package to be compared; comparing the first file information set and the second file information set to obtain a file comparison result; and generating difference information for file pairs with differences when the file comparison result indicates that there are differences between the first file information set and the second file information set.
[0007] Optionally, the step of traversing the first file package to be compared under the file path using a traversal algorithm to generate a first file information set for each file in the first file package to be compared includes: scanning the first file package to be compared; establishing a first node mapping table based on the scanned file directory, wherein the first node mapping table includes the mapping relationship between each file package and each node in the node set, the nodes are used to indicate the location of the file package to be traversed, and the nodes in the node set include at least a root node; traversing the first file package to be compared starting from the root node based on the first node mapping table, and determining the node type of each node; if the node type of the current node is a sub-file package, traversing the current node as a new root node, or if the node type of the current node is a file, ending the traversal to obtain a first target file set, the first target file set storing multiple first target files under the first file package to be compared that need to be compared for difference comparison; and determining the first file information set corresponding to the first file package to be compared based on the first target file set obtained by traversal.
[0008] Optionally, the step of determining the first file information set corresponding to the first file package to be compared based on the first target file set obtained through traversal includes: parsing the first target file set to obtain the file type of each first target file; preprocessing each first target file based on the file type; obtaining the file path and file name of the preprocessed first target file, and using the file path and file name as first primary key information to obtain a first primary key information set; obtaining the file size and file hash value of the preprocessed first target file, and using the file size and file hash value as first verification values to obtain a first verification value set; and storing the first primary key information set and the first verification value set in the first file information set.
[0009] Optionally, the step of traversing the second file package to be compared under the file path using a traversal algorithm to generate a second file information set for each file in the second file package to be compared includes: scanning the second file package to be compared; establishing a second node mapping table based on the scanned file directory, wherein the second node mapping table includes a mapping relationship between file packages and nodes, wherein nodes are used to indicate file packages that need to be traversed; traversing the second file package to be compared starting from the root node based on the second node mapping table, and determining the node type of each node; if the node type of the current node is a sub-file package, traversing the current node as the new root node, or if the node type of the current node is a file, ending the traversal to obtain a second target file set, wherein the second target file set stores multiple second target files under the second file package to be compared that need to be compared for difference comparison; and determining the second file information set corresponding to the second file package to be compared based on the second target file set obtained by traversal.
[0010] Optionally, the step of determining the second file information set corresponding to the second file package to be compared based on the second target file set obtained by traversal includes: parsing the second target file set and determining the file type of each second target file; preprocessing each second target file based on the file type; obtaining the file path and file name of the preprocessed second target file, and using the file path and file name as second primary key information to obtain a second primary key information set; obtaining the file size and file hash value of the preprocessed second target file, and using the file size and file hash value as second verification values to obtain a second verification value set; and storing the second primary key information set and the second verification value set in the second file information set.
[0011] Optionally, the step of comparing the first file information set and the second file information set to obtain a file comparison result includes: comparing the first primary key information in the first file information set with the second primary key information in the second file information set; and if the first primary key information and the second primary key information are the same, comparing the first verification value corresponding to the first primary key information with the second verification value corresponding to the second primary key information to obtain the file comparison result.
[0012] Optionally, when the file comparison result indicates that there is a difference between the first file information set and the second file information set, the step of generating difference information for the file pairs with differences includes: when the file comparison result indicates that there is a difference between the first file information set and the second file information set, confirming that there is a difference between the file pairs composed of the two files being compared; marking the file pairs with differences, and generating difference information for the file pairs with differences based on the file path, file name, file size, and file hash value of the file pairs with differences.
[0013] Optionally, after generating the difference information for the file pairs that have differences, the process includes: deleting files in the first file pair to be compared if the difference information indicates that the first file pair to be compared contains files that do not exist in the second file pair to be compared; adding files in the first file pair to be compared if the difference information indicates that the second file pair to be compared contains files that do not exist in the first file pair to be compared; and modifying files in the first file pair to be compared if the difference information indicates that the first verification value of a file in the first file pair to be compared is different from the second verification value of a file in the second file pair to be compared.
[0014] According to another aspect of the present invention, a file comparison apparatus based on a traversal algorithm is also provided, comprising: an acquisition unit, configured to acquire file paths of a first file package to be compared and a second file package to be compared, wherein the first file package to be compared is a file package to be checked, and the second file package to be compared is a reference file package corresponding to the first file package to be compared; a traversal unit, configured to traverse the first file package to be compared and the second file package to be compared under the file paths using a traversal algorithm, generating a first file information set for each file in the first file package to be compared and a second file information set for each file in the second file package to be compared; a comparison unit, configured to compare the first file information set and the second file information set to obtain a file comparison result; and a generation unit, configured to generate difference information for file pairs that have differences when the file comparison result indicates that there are differences between the first file information set and the second file information set.
[0015] Optionally, the traversal unit includes: a first scanning module, configured to scan the first file package to be compared, and establish a first node mapping table based on the scanned file directory, wherein the first node mapping table includes the mapping relationship between each file package and each node in the node set, the node is used to indicate the position of the file package to be traversed, and the nodes in the node set include at least a root node; a first traversal module, configured to traverse the first file package to be compared starting from the root node based on the first node mapping table, and determine the node type of each node; a first determination module, configured to, when the node type of the current node is a sub-file package, use the current node as the new root node for traversal, or, when the node type of the current node is a file, end the traversal to obtain a first target file set, the first target file set storing multiple first target files under the first file package to be compared that need to be compared for difference comparison; and a first determination module, configured to, based on the first target file set obtained by traversal, determine the first file information set corresponding to the first file package to be compared.
[0016] Optionally, the first determining module includes: a first parsing submodule, configured to parse the first target file set to obtain the file type of each first target file; a first processing submodule, configured to preprocess each first target file based on the file type; a first obtaining submodule, configured to obtain the file path and file name of the preprocessed first target file, and use the file path and file name as first primary key information to obtain a first primary key information set; a second obtaining submodule, configured to obtain the file size and file hash value of the preprocessed first target file, and use the file size and file hash value as first verification values to obtain a first verification value set; and a first storage submodule, configured to store the first primary key information set and the first verification value set in the first file information set.
[0017] Optionally, the traversal unit further includes: a second scanning module, used to scan the second file package to be compared, and establish a second node mapping table based on the scanned file directory, wherein the second node mapping table includes a mapping relationship between file packages and nodes, wherein nodes are used to indicate file packages that need to be traversed; a second traversal module, used to traverse the second file package to be compared starting from the root node based on the second node mapping table, and determine the node type of each node; a second determination module, used to, when the node type of the current node is a sub-file package, use the current node as the new root node for traversal, or, when the node type of the current node is a file, end the traversal to obtain a second target file set, wherein the second target file set stores multiple second target files under the second file package to be compared that need to be compared for difference comparison; and a second determination module, used to determine the second file information set corresponding to the second file package to be compared based on the second target file set obtained by traversal.
[0018] Optionally, the second determining module includes: a second parsing submodule, used to parse the second target file set and determine the file type of each second target file; a second processing submodule, used to preprocess each second target file based on the file type; a third obtaining submodule, used to obtain the file path and file name of the preprocessed second target file, and use the file path and file name as second primary key information to obtain a second primary key information set; a fourth obtaining submodule, used to obtain the file size and file hash value of the preprocessed second target file, and use the file size and file hash value as second verification values to obtain a second verification value set; and a second storage submodule, used to store the second primary key information set and the second verification value set in the second file information set.
[0019] Optionally, the comparison unit includes: a first comparison module, used to compare the first primary key information in the first file information set with the second primary key information in the second file information set; and a second comparison module, used to compare the first verification value corresponding to the first primary key information with the second verification value corresponding to the second primary key information when the first primary key information and the second primary key information are the same, to obtain the file comparison result.
[0020] Optionally, the generation unit includes: a first confirmation module, configured to confirm that there is a difference between the two files in the file pair when the file comparison result indicates that there is a difference between the first file information set and the second file information set; and a first generation module, configured to mark the file pairs with differences and generate difference information of the file pairs with differences based on the file path, file name, file size and file hash value of the file pairs with differences.
[0021] Optionally, the file comparison device based on the traversal algorithm includes: a first deletion module, used to delete files in the first file package to be compared when the difference information indicates that the first file package to be compared contains files that do not exist in the second file package to be compared; a first addition module, used to add files in the first file package to be compared when the difference information indicates that the second file package to be compared contains files that do not exist in the first file package to be compared; and a first modification module, used to modify files in the first file package to be compared when the difference information indicates that the first verification value of a file in the first file package to be compared is different from the second verification value of a file in the second file package to be compared.
[0022] According to another aspect of the present invention, a computer-readable storage medium is also provided, the computer-readable storage medium including a stored computer program, wherein, when the computer program is executed, it controls the device where the computer-readable storage medium is located to execute any of the above-described file comparison methods based on traversal algorithms.
[0023] According to another aspect of the present invention, an electronic device is also provided, including one or more processors and a memory, the memory being used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to implement any of the above-described file comparison methods based on traversal algorithms.
[0024] In this disclosure, the following steps are adopted: First, the file paths of the first file package to be compared and the second file package to be compared are obtained. Then, a traversal algorithm is used to traverse the first file package to be compared and the second file package to be compared under the file paths to generate a first file information set of each file in the first file package to be compared and a second file information set of each file in the second file package to be compared. Then, the first file information set and the second file information set are compared to obtain the file comparison result. Finally, if the file comparison result indicates that there is a difference between the first file information set and the second file information set, difference information of the file pairs with differences is generated.
[0025] In this disclosure, when performing file comparison, for file packages with complex internal structures and a large number of files, a traversal algorithm is used to simultaneously traverse the two file packages to be compared, obtaining a set of file information. The file comparison result is obtained by comparing the set of file information, which supports fast comparison of large batches of files and outputs a set of difference information, making the result clear and intuitive. In addition, file package comparison is automatically performed by inputting file paths, eliminating the need for manual operation and improving comparison efficiency. This solves the technical problem in related technologies that the method of file comparison by manual operation is not suitable for comparing complex and large batches of files. Attached Figure Description
[0026] The accompanying drawings, which are included to provide a further understanding of the invention and form part of this application, illustrate exemplary embodiments of the invention and, together with their description, serve to explain the invention and do not constitute an undue limitation thereof. In the drawings:
[0027] Figure 1 This is a flowchart of an optional file comparison method based on a traversal algorithm according to an embodiment of the present invention;
[0028] Figure 2 This is a flowchart of an optional file package difference comparison method according to an embodiment of the present invention;
[0029] Figure 3 This is a schematic diagram of an optional file comparison device based on a traversal algorithm according to an embodiment of the present invention;
[0030] Figure 4 This is a hardware structure block diagram of an electronic device (or mobile device) based on a file comparison method using a traversal algorithm according to an embodiment of the present invention. Detailed Implementation
[0031] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.
[0032] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0033] To facilitate understanding of the present invention by those skilled in the art, some terms or nouns involved in the various embodiments of the present invention are explained below:
[0034] zip, a common compressed file format;
[0035] JAR is a common compressed file format.
[0036] Depth (breadth) first traversal is a traversal algorithm for tree-like data structures.
[0037] A Map collection is a key-value pair collection used in programs to store data, where one key corresponds to one value.
[0038] Multithreading allows multiple threads to execute tasks concurrently through a program.
[0039] The MD5 value is generated by converting data into a 128-bit hash value using the MD5 algorithm.
[0040] MD5 collision: Two different files calculate the same MD5 value.
[0041] It should be noted that the file comparison method and apparatus based on the traversal algorithm in this disclosure can be used in the field of financial technology to perform difference comparison of files in a file package, and can also be used in any field other than financial technology to perform difference comparison of files in a file package. This disclosure does not limit the application field of the file comparison method and apparatus based on the traversal algorithm.
[0042] It should be noted that all information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, and displayed data) disclosed herein are information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, use, and processing of such data must comply with the relevant laws, regulations, and standards of the relevant countries and regions, and corresponding access points are provided for users to choose to authorize or refuse. For example, this system has interfaces with relevant users or organizations. Before obtaining relevant information, a request to obtain the information must be sent to the aforementioned user or organization through the interface, and the relevant information will be obtained only after receiving consent from the aforementioned user or organization.
[0043] The following embodiments of the present invention can be applied to various file comparison systems / applications / devices. For file packages with complex folder structures and a large number of files, such as program version packages, a traversal algorithm is adopted to automatically traverse from top to bottom (or from bottom to top) with the file package as the root node, obtain the file information set of each file stored in the file package, and compare the file information sets to obtain the differences between the two file packages. It supports fast comparison of large batches of files and outputs the difference information set, making the results clear and intuitive. In addition, file package comparison is automatically performed by inputting the file path, without manual operation, thus improving the efficiency of file comparison.
[0044] The present invention will now be described in detail with reference to various embodiments.
[0045] Example 1
[0046] According to an embodiment of the present invention, an embodiment of a file comparison method based on a traversal algorithm is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0047] Figure 1 This is a flowchart of an optional file comparison method based on a traversal algorithm according to an embodiment of the present invention, such as... Figure 1 As shown, the method includes the following steps:
[0048] Step S101: Obtain the file paths of the first file package to be compared and the second file package to be compared, wherein the first file package to be compared is the file package to be checked, and the second file package to be compared is the reference file package corresponding to the first file package to be compared.
[0049] Step S102: Use a traversal algorithm to traverse the first and second file packages to be compared under the file path to generate a first set of file information for each file in the first file package and a second set of file information for each file in the second file package.
[0050] Step S103: Compare the first file information set and the second file information set to obtain the file comparison result;
[0051] Step S104: If the file comparison result indicates that there is a difference between the first file information set and the second file information set, generate the difference information of the file pairs that have differences.
[0052] Through the above steps, the file paths of the first and second file packages to be compared are obtained, wherein the first file package to be compared is the file package to be checked, and the second file package to be compared is the reference file package corresponding to the first file package to be compared; a traversal algorithm is used to traverse the first and second file packages to be compared under the file paths, generating a first file information set for each file in the first file package to be compared and a second file information set for each file in the second file package to be compared; the first file information set and the second file information set are compared to obtain the file comparison result; if the file comparison result indicates that there is a difference between the first file information set and the second file information set, difference information of the file pairs with differences is generated.
[0053] In this embodiment, when performing file comparison, for file packages with complex internal structures and a large number of files, a traversal algorithm is used to simultaneously traverse the two file packages to be compared, obtaining a set of file information. The file comparison result is obtained by comparing the set of file information, which supports fast comparison of large batches of files and outputs a set of difference information, making the result clear and intuitive. In addition, file package comparison is automatically performed by inputting the file path, without manual operation, which improves the efficiency of file comparison. This solves the technical problem in related technologies that the method of file comparison by manual operation is not suitable for comparing complex and large batches of files.
[0054] The embodiments of the present invention will now be described in detail with reference to the steps described above.
[0055] Step S101: Obtain the file paths of the first and second comparison file packages.
[0056] It should be noted that there is a need for comparing files in many application scenarios, such as comparing documents to find errors, comparing code to check for mistakes, and comparing images to find differences. This is especially true for some program version packages, which have complex structures and carry a large number of files and data. Existing methods that rely on manual comparison or small comparison tools are inefficient and have low accuracy. This embodiment of the invention uses a traversal algorithm to automatically traverse the files in the package, obtain information for each file, and thus compare two packages to determine whether there are differences between them. If differences exist, the difference information between the two files is output to the interface for display.
[0057] It should be noted that the implementation subject of this embodiment of the invention is a file comparison system. Before comparison, it is necessary to determine the two file packages to be compared, including a first file package to be checked and a second file package to be compared for reference. For example, the differences between two program version packages are compared to determine the changes between them and to determine the content that needs to be updated or upgraded. During the comparison process, the old program version package is used as the reference file package to determine the differences between the new version file package and it.
[0058] Step S102: Use a traversal algorithm to traverse the first and second file packages to be compared under the file path to generate a first set of file information for each file in the first file package to be compared and a second set of file information for each file in the second file package to be compared.
[0059] Optionally, the traversal algorithm in this embodiment includes, but is not limited to, depth-first traversal algorithm and breadth-first traversal algorithm. Both are tree-structured traversal algorithms. Since the internal structure of the file package is complex, the internal structure of the file package can be sorted out first, with the outermost file package as the root node and its sub-file packages as nodes, and each node can be traversed.
[0060] Optionally, before traversing the file packages to be compared, two comparison threads are established. The two comparison threads simultaneously traverse each node in the first and second file packages to be compared, thereby improving the efficiency of file comparison.
[0061] Optionally, the step of traversing the first file package to be compared under the file path using a traversal algorithm to generate a first file information set for each file in the first file package to be compared includes: scanning the first file package to be compared; establishing a first node mapping table based on the scanned file directory, wherein the first node mapping table includes the mapping relationship between each file package and each node in the node set, and the nodes are used to indicate the position of the file package to be traversed, and the nodes in the node set include at least a root node; traversing the first file package to be compared starting from the root node based on the first node mapping table, and determining the node type of each node; if the node type of the current node is a sub-file package, traversing the current node as the new root node, or if the node type of the current node is a file, ending the traversal to obtain a first target file set, the first target file set storing multiple first target files under the first file package to be compared that need to be compared for difference comparison; and determining the first file information set corresponding to the first file package to be compared based on the first target file set obtained by traversal.
[0062] It should be noted that a first-node mapping table needs to be established before comparing file packages. The first-node mapping table contains the mapping relationship between the location of each file package or file and the nodes. One node corresponds to one file package or file. Starting from the root node, each node is traversed. If the node type of the current node indicates that the location stores a file package, then the file package is taken as a new root node, and the files under the root node are traversed. This process is repeated. If the current node type indicates that the location stores a file, then the file at that location is parsed to obtain the file information.
[0063] Optionally, the step of determining the first file information set corresponding to the first file package to be compared based on the first target file set obtained by traversal includes: parsing the first target file set to obtain the file type of each first target file; preprocessing each first target file based on the file type; obtaining the file path and file name of the preprocessed first target file, and using the file path and file name as the first primary key information to obtain the first primary key information set; obtaining the file size and file hash value of the preprocessed first target file, and using the file size and file hash value as the first verification value to obtain the first verification value set; and storing the first primary key information set and the first verification value set in the first file information set.
[0064] It should be noted that when the node type of the current node is determined to be a file, it is determined whether the file needs to be filtered. If it is a file to be filtered, it is skipped and not processed. If the file does not need to be filtered, it needs to be parsed first to obtain the file type, and the file is preprocessed based on the file type. For example, compressed files need to be decompressed first, and encrypted files need to be decrypted first.
[0065] It should be noted that after obtaining the preprocessed file, the file information is extracted. The file information includes the file path, file name, file size, and file hash value. The file hash value is obtained by extracting the digest information from the file content and converting the digest information into a 128-bit hash value using the MD5 algorithm.
[0066] For example, the steps to calculate the hash value of the string "Hello World!" using the MD5 algorithm are as follows:
[0067] The original string is padded to make its length a multiple of 64. In this case, "Hello World!" has a length of 12, so 52 bytes of padding are needed to make its total length 64. The padding method is to add a 1 followed by several 0s to the end of the string until the length is 64.
[0068] Divide the padded string into 512-bit data blocks;
[0069] Initialize the values of the four registers A, B, C, and D for the MD5 algorithm;
[0070] Each data block is processed through 4 rounds of looping. In each round of looping, the value of the register is transformed once. The transformation methods include bit shifting, logical operation, and modular operation.
[0071] The final values of the four registers A, B, C, and D form a 128-bit hash value, which is the MD5 value of "Hello World!". The final MD5 value of "Hello World!" is: b94d27b9934d3e08a52e52d7da7dab fac484efe37a5380ee9088f7ace2efcde9.
[0072] After obtaining the file information, the file path and file name are used as the first primary key information, and the file size and file hash value are used as the first verification value. The first primary key information and the first verification value are stored together in the first file information set, and the first primary key information and the first verification value in the first file information set are in one-to-one correspondence.
[0073] Optionally, the step of traversing the second file package to be compared under the file path using a traversal algorithm to generate a second file information set for each file in the second file package to be compared includes: scanning the second file package to be compared; establishing a second node mapping table based on the scanned file directory, wherein the second node mapping table includes the mapping relationship between file packages and nodes, and nodes are used to indicate the file packages to be traversed; traversing the second file package to be compared starting from the root node based on the second node mapping table, and determining the node type of each node; if the node type of the current node is a sub-file package, traversing the current node as the new root node, or if the node type of the current node is a file, ending the traversal to obtain a second target file set, which stores multiple second target files under the second file package to be compared that need to be compared for difference; and determining the second file information set corresponding to the second file package to be compared based on the second target file set obtained by traversal.
[0074] Optionally, the step of determining the second file information set corresponding to the second file package to be compared based on the second target file set obtained by traversal includes: parsing the second target file set and determining the file type of each second target file; preprocessing each second target file based on the file type; obtaining the file path and file name of the preprocessed second target file, and using the file path and file name as the second primary key information to obtain the second primary key information set; obtaining the file size and file hash value of the preprocessed second target file, and using the file size and file hash value as the second verification value to obtain the second verification value set; and storing the second primary key information set and the second verification value set in the second file information set.
[0075] It should be noted that the steps for traversing and obtaining information from the second file package to be compared are the same as those for the first file package to be compared, and are performed simultaneously by two threads.
[0076] Step S103: Compare the first file information set and the second file information set to obtain the file comparison result.
[0077] Optionally, the step of comparing the first file information set and the second file information set to obtain the file comparison result includes: comparing the first primary key information in the first file information set with the second primary key information in the second file information set; if the first primary key information and the second primary key information are the same, comparing the first verification value corresponding to the first primary key information with the second verification value corresponding to the second primary key information to obtain the file comparison result.
[0078] It should be noted that during the comparison, the obtained first and second information sets need to be traversed. The difference between the two files is determined by comparing the primary key information and the check value. First, the primary key information in the first set of information is compared with the secondary primary key information in the second set of information. If the primary key information and the secondary primary key information are the same, it means that the primary key information corresponds to a pair of related files in the two file packages to be compared. Then, the check value of the file pair is compared. If the primary check value and the secondary check value of the two files are different, it indicates that there is a difference between the file pair.
[0079] Step S104: If the file comparison result indicates that there is a difference between the first file information set and the second file information set, generate the difference information of the file pairs that have differences.
[0080] Optionally, when the file comparison result indicates that there is a difference between the first file information set and the second file information set, the step of generating difference information for the file pairs with differences includes: confirming that there is a difference between the file pairs composed of the two files being compared when the file comparison result indicates that there is a difference between the first file information set and the second file information set; marking the file pairs with differences; and generating difference information for the file pairs with differences based on the file path, file name, file size, and file hash value of the file pairs with differences.
[0081] Optionally, after generating the difference information for the file pairs that have differences, the process includes: deleting files in the first file pair to be compared if the difference information indicates that the first file pair to be compared contains files that do not exist in the second file pair to be compared; adding files in the first file pair to be compared if the difference information indicates that the second file pair to be compared contains files that do not exist in the first file pair to be compared; and modifying files in the first file pair to be compared if the difference information indicates that the first verification value of a file in the first file pair to be compared is different from the second verification value of a file in the second file pair to be compared.
[0082] It should be noted that after obtaining the difference files and their corresponding difference information, the file package to be checked needs to be added, modified, or deleted. This includes: if the first file package to be compared contains a file that is not in the second file package, then the redundant file in the first file package to be compared is deleted; if the first file package to be compared is missing a file stored in the second file package to be compared, then that file is added to the first file package to be compared; if a file in the first file package to be compared has a different value (verification value) from the corresponding file in the second file package to be compared, then that file in the first file package to be compared is modified.
[0083] Optionally, embodiments of the present invention can also extract the file content of a specific file, compare the file content byte by byte, and obtain the difference information between file pairs.
[0084] The present invention will now be described in conjunction with a more specific embodiment.
[0085] This invention provides a method for comparing file differences in a file package, such as a program version package or other file package with a complex folder structure and a large number of files. The method uses a depth-first search algorithm or a breadth-first search algorithm to automatically traverse the file directory, obtain the file name and path of all files, as well as the file size and MD5 value, and store them in two Map datasets. Then, the two datasets are traversed and compared to output all the different files in the two file packages, and operations such as adding, modifying, and deleting files are performed.
[0086] Figure 2 This is a flowchart of an optional file package difference comparison method according to an embodiment of the present invention, such as... Figure 2 As shown, the file package difference comparison method includes the following steps:
[0087] Step 1: Begin by creating thread A and thread B;
[0088] Step 2: Obtain file package A (corresponding to the first file package to be compared mentioned above) and file package B (corresponding to the second file package to be compared mentioned above);
[0089] Step 3: Traverse file package A and file package B to obtain two Map sets (corresponding to the first file information set and the second file information set mentioned above);
[0090] Step 201: Start scanning from the root node;
[0091] Step 202: Determine whether the current node stores a file package. If yes, proceed to step 203; otherwise, proceed to step 204.
[0092] Step 203: Continue scanning with the current node as the root node;
[0093] Step 204: Determine whether the current file needs to be filtered. If not, proceed to step 205; if yes, proceed to step 206.
[0094] Step 205: Determine if the current file is a compressed file. If yes, proceed to step 207; otherwise, proceed to step 208.
[0095] Step 206: Skip;
[0096] Step 207: Determine the compression type and decompress the file;
[0097] Step 208: Obtain the file path and file name of the file and use them as the key (corresponding to the primary key information mentioned above);
[0098] Step 209: Obtain the file size and MD5 value (corresponding to the hash value above) and use them as the value (corresponding to the collation value above);
[0099] Step 210: Store the key and value in a Map collection to obtain two Map collections based on the two file packages to be compared;
[0100] Step 4: Iterate through the two Map collections;
[0101] Step 5: Determine the key and value;
[0102] Step Six: Output the difference information;
[0103] Step 7: Process the difference files.
[0104] Through the above steps: This embodiment of the invention first scans the file packages, requiring input of the file paths (including relative or absolute paths) of the two file packages to be compared. A multi-threaded traversal algorithm is used to simultaneously traverse the two file packages, obtaining all filenames and their paths within each package, as well as file sizes and MD5 values. Starting from the root node, each scanned node is evaluated. If the node is a folder, its child nodes are recursively scanned; if it is a file, the file type is obtained, and it is determined whether the file needs filtering. If filtering is required, it is skipped. If comparison is needed, it is first determined whether the file is a compressed file type (e.g., zip, 7z, war, etc.). If so, the corresponding algorithm is matched according to the compressed file type to automatically complete the decompression before continuing the traversal. The relative paths and filenames of the files to be compared obtained during the traversal are used as keys, and the file sizes and calculated MD5 values are used as values and stored in a Map collection. The inclusion of file size as a comparison criterion is to prevent low-probability MD5 collisions.
[0105] After traversing both file packages, the two generated Map collections are iterated and compared to find the values (file size and MD5 value) corresponding to the same key. For example, in file packages A and B (reference file packages), files that exist in A but not in B are marked as deleted, and those that do are marked as added. Files with different values are marked as modified. During the comparison process, the results are saved to the result Map collection and finally output to the front-end interface. The results can be filtered and viewed according to whether they are added, modified, or deleted, or exported to an Excel file.
[0106] Through the above embodiments, for file packages with complex folder structures and a large number of files, such as program version packages, a traversal algorithm is adopted to automatically traverse from top to bottom with the file package as the root node, obtain the file information set of each file stored in the file package, and compare the file information sets to obtain the differences between the two file packages. This supports fast comparison of a large number of files and outputs the difference information set, making the results clear and intuitive.
[0107] At the same time, file packages are automatically compared by inputting file paths, eliminating the need for manual operation.
[0108] The following is a detailed description with reference to another embodiment.
[0109] Example 2
[0110] The file comparison device based on the traversal algorithm provided in this embodiment includes multiple implementation units, each of which corresponds to a specific implementation step in Embodiment 1 above.
[0111] Figure 3 This is a schematic diagram of an optional file comparison device based on a traversal algorithm according to an embodiment of the present invention, such as... Figure 3 As shown, the file comparison device may include: an acquisition unit 31, a traversal unit 32, a comparison unit 33, and a generation unit 34, wherein,
[0112] The acquisition unit 31 is used to acquire the file paths of the first file package to be compared and the second file package to be compared, wherein the first file package to be compared is the file package to be checked, and the second file package to be compared is the reference file package corresponding to the first file package to be compared;
[0113] The traversal unit 32 is used to traverse the first file package to be compared and the second file package to be compared under the file path using a traversal algorithm, and generate a first file information set of each file in the first file package to be compared and a second file information set of each file in the second file package to be compared.
[0114] The comparison unit 33 is used to compare the first file information set and the second file information set to obtain the file comparison result;
[0115] The generation unit 34 is used to generate difference information for file pairs that have differences when the file comparison result indicates that there are differences between the first file information set and the second file information set.
[0116] The aforementioned file comparison device based on a traversal algorithm acquires the file paths of the first and second file packages to be compared by the acquisition unit 31; the traversal unit 32 uses a traversal algorithm to traverse the first and second file packages to be compared under the file paths, generating a first set of file information for each file in the first file package and a second set of file information for each file in the second file package; the comparison unit 33 compares the first and second file information sets to obtain the file comparison result; and the generation unit 34 generates difference information for the file pairs that have differences when the file comparison result indicates that there are differences between the first and second file information sets.
[0117] In this embodiment, when performing file comparison, for file packages with complex internal structures and a large number of files, a traversal algorithm is used to simultaneously traverse the two file packages to be compared, obtaining a set of file information. The file comparison result is obtained by comparing the set of file information, which supports fast comparison of a large number of files and outputs a set of difference information, making the result clear and intuitive. In addition, file package comparison is automatically performed by inputting the file path, without manual operation. This solves the technical problem in related technologies that the method of file comparison by manual operation is not suitable for comparing files with complex structures and large numbers of files.
[0118] Optionally, the traversal unit 32 includes: a first scanning module, used to scan the first file package to be compared, and establish a first node mapping table based on the scanned file directory, wherein the first node mapping table includes the mapping relationship between each file package and each node in the node set, the nodes are used to indicate the position of the file package to be traversed, and the nodes in the node set include at least: a root node; a first traversal module, used to traverse the first file package to be compared starting from the root node based on the first node mapping table, and determine the node type of each node; a first judgment module, used to traverse the current node as the new root node when the node type of the current node is a sub-file package, or to end the traversal to obtain a first target file set when the node type of the current node is a file, the first target file set storing multiple first target files under the first file package to be compared that need to be compared for difference comparison; and a first determination module, used to determine the first file information set corresponding to the first file package to be compared based on the first target file set obtained by traversal.
[0119] Optionally, the first determining module includes: a first parsing submodule, used to parse the first target file set to obtain the file type of each first target file; a first processing submodule, used to preprocess each first target file based on the file type; a first obtaining submodule, used to obtain the file path and file name of the preprocessed first target file, and use the file path and file name as the first primary key information to obtain a first primary key information set; a second obtaining submodule, used to obtain the file size and file hash value of the preprocessed first target file, and use the file size and file hash value as the first verification value to obtain a first verification value set; and a first storage submodule, used to store the first primary key information set and the first verification value set in the first file information set.
[0120] Optionally, the traversal unit 32 further includes: a second scanning module, used to scan the second file package to be compared, and establish a second node mapping table based on the scanned file directory, wherein the second node mapping table includes the mapping relationship between file packages and nodes, wherein nodes are used to indicate the file packages to be traversed; a second traversal module, used to traverse the second file package to be compared starting from the root node based on the second node mapping table, and determine the node type of each node; a second judgment module, used to traverse the current node as the new root node when the node type of the current node is a sub-file package, or to end the traversal to obtain a second target file set when the node type of the current node is a file, wherein the second target file set stores multiple second target files under the second file package to be compared that need to be compared for difference; and a second determination module, used to determine the second file information set corresponding to the second file package to be compared based on the second target file set obtained by traversal.
[0121] Optionally, the second determining module includes: a second parsing submodule, used to parse the second target file set and determine the file type of each second target file; a second processing submodule, used to preprocess each second target file based on the file type; a third obtaining submodule, used to obtain the file path and file name of the preprocessed second target file, and use the file path and file name as the second primary key information to obtain a second primary key information set; a fourth obtaining submodule, used to obtain the file size and file hash value of the preprocessed second target file, and use the file size and file hash value as the second verification value to obtain a second verification value set; and a second storage submodule, used to store the second primary key information set and the second verification value set in the second file information set.
[0122] Optionally, the comparison unit 33 includes: a first comparison module, used to compare the first primary key information in the first file information set with the second primary key information in the second file information set; and a second comparison module, used to compare the first verification value corresponding to the first primary key information with the second verification value corresponding to the second primary key information when the first primary key information and the second primary key information are the same, to obtain a file comparison result.
[0123] Optionally, the generation unit 34 includes: a first confirmation module, used to confirm that there is a difference between the two files in the file pair being compared when the file comparison result indicates that there is a difference between the first file information set and the second file information set; and a first generation module, used to mark the file pairs with differences and generate difference information of the file pairs with differences based on the file path, file name, file size and file hash value of the file pairs with differences.
[0124] Optionally, the file comparison device based on the traversal algorithm includes: a first deletion module, used to delete files in the first file package to be compared when the difference information indicates that the first file package to be compared contains files that do not exist in the second file package to be compared; a first addition module, used to add files in the first file package to be compared when the difference information indicates that the second file package to be compared contains files that do not exist in the first file package to be compared; and a first modification module, used to modify files in the first file package to be compared when the difference information indicates that the first verification value of a file in the first file package to be compared is different from the second verification value of a file in the second file package to be compared.
[0125] The aforementioned file comparison device based on the traversal algorithm may also include a processor and a memory. The aforementioned acquisition unit 31, traversal unit 32, comparison unit 33, generation unit 34, etc., are all stored in the memory as program units, and the processor executes the aforementioned program units stored in the memory to realize the corresponding functions.
[0126] The aforementioned processor contains a kernel, which retrieves the corresponding program units from memory. One or more kernels can be configured, and kernel parameters can be adjusted to perform differences between file packages.
[0127] The aforementioned memory may include non-permanent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.
[0128] According to another aspect of the present invention, a computer-readable storage medium is also provided, the computer-readable storage medium including a stored computer program, wherein, when the computer program is running, it controls the device where the computer-readable storage medium is located to execute any of the above-described file comparison methods based on traversal algorithms.
[0129] According to another aspect of the present invention, an electronic device is also provided, including one or more processors and a memory, the memory being used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to implement any of the above-described file comparison methods based on traversal algorithms.
[0130] This application also provides a computer program product, which, when executed on a data processing device, is suitable for executing an initialization program with the following method steps: obtaining the file paths of a first file package to be compared and a second file package to be compared, wherein the first file package to be compared is the file package to be checked, and the second file package to be compared is the reference file package corresponding to the first file package to be compared; traversing the first file package to be compared and the second file package to be compared under the file paths using a traversal algorithm to generate a first file information set for each file in the first file package to be compared and a second file information set for each file in the second file package to be compared; comparing the first file information set and the second file information set to obtain a file comparison result; and generating difference information for the file pairs with differences when the file comparison result indicates that there are differences between the first file information set and the second file information set.
[0131] Figure 4 This is a hardware structure block diagram of an electronic device (or mobile device) based on a file comparison method using a traversal algorithm according to an embodiment of the present invention. Figure 4 As shown, an electronic device may include one or more ( Figure 4 The processor 402 (which may include, but is not limited to, a microprocessor MCU or a programmable logic device FPGA, etc.) and the memory 404 for storing data may also be included. In addition, it may include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of the I / O interface), a network interface, a keyboard, a power supply, and / or a camera. Those skilled in the art will understand that... Figure 4 The structure shown is for illustrative purposes only and does not limit the structure of the electronic device described above. For example, the electronic device may also include components that are more... Figure 4 The more or fewer components shown, or having the same Figure 4 The different configurations shown.
[0132] The sequence numbers of the above embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0133] In the above embodiments of the present invention, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0134] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units can be a logical functional division, and in actual implementation, there may be other division methods. For instance, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual coupling, direct coupling, or communication connection may be through some interfaces; the indirect coupling or communication connection between units or modules may be electrical or other forms.
[0135] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0136] Furthermore, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0137] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, read-only memory (ROM), random access memory (RAM), portable hard drives, magnetic disks, or optical disks.
[0138] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A file comparison method based on a traversal algorithm, characterized in that, include: Obtain the file paths of the first comparison file package and the second comparison file package, wherein the first comparison file package is the file package to be checked, and the second comparison file package is the reference file package corresponding to the first comparison file package; A traversal algorithm is used to traverse the first and second file packages to be compared under the file path, generating a first file information set for each file in the first file package to be compared and a second file information set for each file in the second file package to be compared. This includes: scanning the first file package to be compared; establishing a first node mapping table based on the scanned file directory, wherein the first node mapping table includes the mapping relationship between each file package and each node in the node set, the nodes indicating the location of the file package to be traversed, and the nodes in the node set including at least a root node; traversing the first file package to be compared starting from the root node based on the first node mapping table, and determining the node type of each node; if the node type of the current node is a sub-file package, traversing the current node as the new root node, or if the node type of the current node is a file, ending the traversal to obtain a first target file set, which stores multiple first target files under the first file package to be compared that need to be compared for difference comparison; and determining the first file information set corresponding to the first file package to be compared based on the traversed first target file set. The step of determining the first file information set corresponding to the first file package to be compared based on the first target file set obtained by traversal includes: parsing the first target file set to obtain the file type of each first target file; preprocessing each first target file based on the file type; obtaining the file path and file name of the preprocessed first target file, and using the file path and file name as first primary key information to obtain a first primary key information set; obtaining the file size and file hash value of the preprocessed first target file, and using the file size and file hash value as first verification values to obtain a first verification value set; and storing the first primary key information set and the first verification value set in the first file information set. The comparison of the first file information set and the second file information set to obtain a file comparison result includes: comparing the first primary key information in the first file information set with the second primary key information in the second file information set; and when the first primary key information and the second primary key information are the same, comparing the first verification value corresponding to the first primary key information with the second verification value corresponding to the second primary key information to obtain the file comparison result. When the file comparison result indicates that there is a difference between the first file information set and the second file information set, generating difference information for the file pairs with differences includes: confirming that there is a difference between the file pairs composed of the two files being compared when the file comparison result indicates that there is a difference between the first file information set and the second file information set; marking the file pairs with differences, and generating difference information for the file pairs with differences based on the file path, file name, file size, and file hash value of the file pairs with differences.
2. The comparison method according to claim 1, characterized in that, The step of traversing the second file package to be compared under the file path using a traversal algorithm to generate a second file information set for each file in the second file package to be compared includes: The second file package to be compared is scanned, and a second node mapping table is established based on the file directory obtained from the scan. The second node mapping table includes the mapping relationship between file packages and nodes, where nodes are used to indicate the file packages that need to be traversed. Based on the second node mapping table, the second file package to be compared is traversed starting from the root node, and the node type of each node is determined. If the node type of the current node is a sub-file package, the current node is used as the new root node for traversal; or, if the node type of the current node is a file, the traversal ends to obtain the second target file set, which stores the files under the second file package to be compared that need to be compared for differences. Based on the second target file set obtained through traversal, the second file information set corresponding to the second file package to be compared is determined.
3. The comparison method according to claim 2, characterized in that, The step of determining the second file information set corresponding to the second comparison file package based on the second target file set obtained by traversal includes: Parse the second set of target files and determine the file type of each second target file; Preprocess each of the second target files based on the file type; Obtain the file path and file name of the preprocessed second target file, and use the file path and file name as the second primary key information to obtain the second primary key information set; Obtain the file size and file hash value of the preprocessed second target file, and use the file size and file hash value as the second verification value to obtain the second verification value set; The second primary key information set and the second verification value set are stored in the second file information set.
4. The comparison method according to claim 1, characterized in that, After generating the difference information for the file pairs that have differences, it includes: If the difference information indicates that the first file package to be compared contains a file that does not exist in the second file package to be compared, a file deletion operation is performed on the file in the first file package to be compared. If the difference information indicates that the second file package to be compared contains a file that does not exist in the first file package to be compared, then a file addition operation is performed on the file in the first file package to be compared. If the difference information indicates that the first verification value of a file in the first file package to be compared is different from the second verification value of a file in the second file package to be compared, a file modification operation is performed on the file in the first file package to be compared.
5. A file comparison device based on a traversal algorithm, characterized in that, include: The acquisition unit is used to acquire the file paths of the first file package to be compared and the second file package to be compared, wherein the first file package to be compared is the file package to be checked, and the second file package to be compared is the reference file package corresponding to the first file package to be compared; The traversal unit is used to traverse the first file package to be compared and the second file package to be compared under the file path using a traversal algorithm, and generate a first file information set for each file in the first file package to be compared and a second file information set for each file in the second file package to be compared; The traversal unit includes: a first scanning module, used to scan the first file package to be compared, and establish a first node mapping table based on the scanned file directory, wherein the first node mapping table includes the mapping relationship between each file package and each node in the node set, the node is used to indicate the position of the file package to be traversed, and the nodes in the node set include at least: a root node; a first traversal module, used to traverse the first file package to be compared starting from the root node based on the first node mapping table, and determine the node type of each node; a first determination module, used to traverse the current node as the new root node when the node type of the current node is a sub-file package, or to end the traversal to obtain a first target file set when the node type of the current node is a file, the first target file set storing multiple first target files under the first file package to be compared that need to be compared for difference comparison; and a first determination module, used to determine the first file information set corresponding to the first file package to be compared based on the first target file set obtained by traversal. The first determining module includes: a first parsing submodule, used to parse the first target file set to obtain the file type of each first target file; a first processing submodule, used to preprocess each first target file based on the file type; a first obtaining submodule, used to obtain the file path and file name of the preprocessed first target file, and use the file path and file name as first primary key information to obtain a first primary key information set; a second obtaining submodule, used to obtain the file size and file hash value of the preprocessed first target file, and use the file size and file hash value as first verification values to obtain a first verification value set; and a first storage submodule, used to store the first primary key information set and the first verification value set in the first file information set. The comparison unit is used to compare the first file information set and the second file information set to obtain a file comparison result; The comparison unit includes: a first comparison module, used to compare the first primary key information in the first file information set with the second primary key information in the second file information set; and a second comparison module, used to compare the first verification value corresponding to the first primary key information with the second verification value corresponding to the second primary key information when the first primary key information and the second primary key information are the same, to obtain the file comparison result. The generation unit is configured to generate difference information for the file pairs that have differences when the file comparison result indicates that there are differences between the first file information set and the second file information set; The generation unit includes: a first confirmation module, used to confirm that there is a difference between the two files in the file pair when the file comparison result indicates that there is a difference between the first file information set and the second file information set; and a first generation module, used to mark the file pairs with differences and generate difference information of the file pairs with differences based on the file path, file name, file size and file hash value of the file pairs with differences.
6. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored computer program, wherein, when the computer program is executed, it controls the device where the computer-readable storage medium is located to perform the file comparison method based on the traversal algorithm as described in any one of claims 1 to 4.
7. An electronic device, characterized in that, It includes one or more processors and a memory, the memory being used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to implement the file comparison method based on the traversal algorithm as described in any one of claims 1 to 4.