func folderVisit(path string, f os.FileInfo, err error) error { if strings.HasSuffix(f.Name(), ".epub") { //Put a request to queue, the max size of queue is CPU number. go convertEPUB(path, targetPath+gotongwen.Convert(f.Name())) count++ } return nil }
func convertEPUB(source string, target string) { //Open zip file r, err := zip.OpenReader(source) if err != nil { log.Fatal(err) } defer r.Close() //Create result zip file targetFile, err := os.Create(target) if err != nil { log.Fatal(err) } zipWriter := zip.NewWriter(targetFile) defer zipWriter.Close() //Travel all file in zip for _, f := range r.File { //Open file in the zip file and create a buffer reader rc, err := f.Open() if err != nil { log.Fatal(err) } zipFileReader := bufio.NewReader(rc) //We only convert xhtml file , other files will be wrote directly. if endWith(f.Name, "xhtml") || endWith(f.Name, "html") || endWith(f.Name, "htm") { //Create a string list from file in zip. lines, _ := knife.ReadLines_FromReader(zipFileReader) //Convert the content by tongweng table lines = handleLines(lines, gotongwen.Convert) //Write to new zip file writeLines_To_ZipFile(lines, zipWriter, f.Name) } else { writeBytes_To_ZipFile(zipFileReader, zipWriter, f.Name) } rc.Close() } result_queue <- gotongwen.Convert(target) }