I have client and server. Client sends archives (zip) as []byte to server. Now server reads them and print all content files names:
r, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
fmt.Println(err)
}
for _, zipFile := range r.File {
if strings.Contains(zipFile.Name, ".tar") {
fmt.Println(zipFile.Name)
}
}
The trouble is archive has nested archives (tar) with xml files inside, so these names are the names of archives. How can I access nested archives content (so i can work with xml files)?
CodePudding user response:
You just have to keep going and access the tar:
if strings.Contains(zipFile.Name, ".tar") {
fmt.Println(zipFile.Name)
rz, err := zipFile.Open()
if err != nil {
fmt.Println(err)
}
tr := tar.NewReader(rz)
for {
hdr, err := tr.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
log.Fatal(err)
}
if strings.Contains(hdr.Name, ".xml") {
fmt.Printf("Contents of %s:\n", hdr.Name)
/*if _, err := io.Copy(os.Stdout, tr); err != nil {
log.Fatal(err)
}
fmt.Println()
*/
}
}
}
You can see the documentation for the tar package. I used the example and put a comment for file access since I don't know what you want to do with the file.