I
I
imbaMF2021-09-08 09:31:06
go
imbaMF, 2021-09-08 09:31:06

Why doesn't tar save Russian filenames, what could be the reason?

Windows 10
Golang 1.17
-----
zip normally displays Russian names, I checked it on another tar archive
-----
I create tar, the code is below, but when I open it with zip, instead of Russian characters, there is emptiness ... I understand that I'm the only one with the problem, but I can't figure out what exactly is wrong. I reinstalled Go and it didn't solve the problem...

package main

import (
  "archive/tar"
  "fmt"
  "io"
  "log"
  "os"
)

func main() {
  // Create and add some files to the archive.
  f, err := os.Create("test.tar")
  if err != nil {
    panic(err)
  }
  tw := tar.NewWriter(f)
  var files = []struct {
    Name, Body string
  }{
    {"eng_ридми.тхт", "Этот архив создан для проверки создания таров из файлов с русскими буковками."},
    {"гоферRU.txt", "Имена Гофера:\nGeorge\nGeoffrey\nGonzo"},
    {"туду.txt", "Добыть лицензию на разведение животных (придурки!)."},
  }
  for _, file := range files {
    hdr := &tar.Header{
      Name: file.Name,
      Mode: 0600,
      Size: int64(len(file.Body)),
    }
    if err := tw.WriteHeader(hdr); err != nil {
      log.Fatal(err)
    }
    if _, err := tw.Write([]byte(file.Body)); err != nil {
      log.Fatal(err)
    }
  }
  if err := tw.Close(); err != nil {
    log.Fatal(err)
  }
  f.Close()

  f, err = os.Open("test.tar")
  if err != nil {
    panic(err)
  }

  // Open and iterate through the files in the archive.
  tr := tar.NewReader(f)
  for {
    hdr, err := tr.Next()
    if err == io.EOF {
      break // End of archive
    }
    if err != nil {
      log.Fatal(err)
    }
    fmt.Printf("Contents of %s:\n", hdr.Name)
    if _, err := io.Copy(os.Stdout, tr); err != nil {
      log.Fatal(err)
    }
    fmt.Println()
  }
  f.Close()
}


613858963cf5c091683273.png
613858a3859c5972423585.png

For example, another tar in zip (Russian text is available)...
61385944115cc327315688.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question