I've written some code that creates a .zip file from a number of other files (.png in case it matters). The zip file seems to be fine, and I can open it on both a mac and windows computer without a problem. However when an Android device (using the java.util.zip package) tries to open the file, it throws an error that the file is not a .zip file. Given how long java.util.zip has been in use, I doubt that the problem is there, and assume the issue is with the method that I've used to create the .zip file in the first place.
func createZip(zipDir, zipName string, filesToZip []string) error {
// creat the file to zip the contents into
newfile, err := os.Create(zipDir + zipName)
if err != nil {
return err
}
defer newfile.Close()
zipit := zip.NewWriter(newfile)
defer zipit.Close()
for _, filename := range filesToZip {
zipFile, err := os.Open(zipDir + filename)
if err != nil {
return err
}
defer zipFile.Close()
// get the file information
info, err := zipFile.Stat()
if err != nil {
return err
}
// create a zip header from the os.FileInfo
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
// write the header to the zip file
writer, err := zipit.CreateHeader(header)
if err != nil {
return err
}
// copy the file to the zip file
_, err = io.Copy(writer, zipFile)
if err != nil {
return err
}
}
return nil
}
I would appreciate feedback on my code, or suggestions as to where I can look for the cause.