I am processing multiple .json
files which I need to add to a single .zip
archive using a package available here: https://github.com/larzconwell/bzip2.
I have referenced other possible solutions and questions related to io.Writer
along with .Close()
and .Flush()
Code that is used:
if processedCounter%*filesInPackage == 0 || filesLeftToProcess == 0 {
// Create empty zip file with numbered filename.
emptyZip, err := os.Create(filepath.Join(absolutePathOutputDirectory, "package_"+strconv.Itoa(packageCounter)+".zip"))
if err != nil {
panic(err)
}
// Get list of .json filenames to be packaged:
listOfProcessedJSON := listFiles(absolutePathInterDirectory, ".json")
bzipWriter, err := bzip2.NewWriterLevel(emptyZip, 1)
if err != nil {
panic(err)
}
defer bzipWriter.Close()
// Add listed files to the archive
for _, file := range listOfProcessedJSON {
// Read byte array from json file:
JSONContents, err := ioutil.ReadFile(file)
if err != nil {
fmt.Printf("Failed to open %s: %s", file, err)
}
// Write a single JSON to .zip:
// Process hangs here!
_, compressionError := bzipWriter.Write(JSONContents)
if compressionError != nil {
fmt.Printf("Failed to write %s to zip: %s", file, err)
compressionErrorCounter++
}
err = bzipWriter.Close()
if err != nil {
fmt.Printf("Failed to Close bzipWriter")
}
}
// Delete intermediate .json files
dir, err := ioutil.ReadDir(absolutePathInterDirectory)
for _, d := range dir {
os.RemoveAll(filepath.Join([]string{"tmp", d.Name()}...))
}
packageCounter++
}
Using debugger it seems that the my program hangs on the following line:
_, compressionError := bzipWriter.Write(JSONContents)
The package itself does not provide usage examples so my knowledge is based on studying documentation, StackOverflow questions, and different available articles e.g.:
https://www.golangprograms.com/go-program-to-compress-list-of-files-into-zip.html
Let me know if anyone knows a possible solution to this problem.