File tree Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "io/ioutil"
5+ "os"
6+ "sort"
7+ "strings"
8+ )
9+
10+ func main () {
11+ dirPath := "." // 当前目录
12+ mergeSplitPDFsInDirectory (dirPath )
13+ }
14+
15+ func mergeSplitPDFsInDirectory (dirPath string ) {
16+ files , err := ioutil .ReadDir (dirPath )
17+ if err != nil {
18+ panic (err )
19+ }
20+
21+ splitFiles := make (map [string ][]string )
22+
23+ for _ , file := range files {
24+ if file .IsDir () {
25+ continue
26+ }
27+ fileName := file .Name ()
28+ if strings .Contains (fileName , ".pdf." ) {
29+ baseName := strings .Split (fileName , ".pdf." )[0 ] + ".pdf"
30+ splitFiles [baseName ] = append (splitFiles [baseName ], fileName )
31+ }
32+ }
33+
34+ for baseName , parts := range splitFiles {
35+ sort .Strings (parts ) // 确保文件顺序正确
36+ mergeFiles (baseName , parts )
37+ }
38+ }
39+
40+ func mergeFiles (baseName string , parts []string ) {
41+ mergedFile , err := os .Create (baseName )
42+ if err != nil {
43+ panic (err )
44+ }
45+ defer mergedFile .Close ()
46+
47+ for _ , part := range parts {
48+ data , err := ioutil .ReadFile (part )
49+ if err != nil {
50+ panic (err )
51+ }
52+ _ , err = mergedFile .Write (data )
53+ if err != nil {
54+ panic (err )
55+ }
56+ os .Remove (part ) // 合并后删除分割文件
57+ }
58+ }
You can’t perform that action at this time.
0 commit comments