mkzip.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2022 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build ignore
  5. // Mkzip writes a zoneinfo.zip with the content of the current directory
  6. // and its subdirectories, with no compression, suitable for package time.
  7. //
  8. // Usage:
  9. //
  10. // go run ../../mkzip.go ../../zoneinfo.zip
  11. //
  12. // We use this program instead of 'zip -0 -r ../../zoneinfo.zip *' to get
  13. // a reproducible generator that does not depend on which version of the
  14. // external zip tool is used or the ordering of file names in a directory
  15. // or the current time.
  16. package main
  17. import (
  18. "archive/zip"
  19. "bytes"
  20. "flag"
  21. "fmt"
  22. "hash/crc32"
  23. "io/fs"
  24. "log"
  25. "os"
  26. "path/filepath"
  27. "strings"
  28. )
  29. func usage() {
  30. fmt.Fprintf(os.Stderr, "usage: go run mkzip.go zoneinfo.zip\n")
  31. os.Exit(2)
  32. }
  33. func main() {
  34. log.SetPrefix("mkzip: ")
  35. log.SetFlags(0)
  36. flag.Usage = usage
  37. flag.Parse()
  38. args := flag.Args()
  39. if len(args) != 1 || !strings.HasSuffix(args[0], ".zip") {
  40. usage()
  41. }
  42. var zb bytes.Buffer
  43. zw := zip.NewWriter(&zb)
  44. seen := make(map[string]bool)
  45. err := filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
  46. if d.IsDir() {
  47. return nil
  48. }
  49. data, err := os.ReadFile(path)
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. if strings.HasSuffix(path, ".zip") {
  54. log.Fatalf("unexpected file during walk: %s", path)
  55. }
  56. name := filepath.ToSlash(path)
  57. w, err := zw.CreateRaw(&zip.FileHeader{
  58. Name: name,
  59. Method: zip.Store,
  60. CompressedSize64: uint64(len(data)),
  61. UncompressedSize64: uint64(len(data)),
  62. CRC32: crc32.ChecksumIEEE(data),
  63. })
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. if _, err := w.Write(data); err != nil {
  68. log.Fatal(err)
  69. }
  70. seen[name] = true
  71. return nil
  72. })
  73. if err != nil {
  74. log.Fatal(err)
  75. }
  76. if err := zw.Close(); err != nil {
  77. log.Fatal(err)
  78. }
  79. if len(seen) == 0 {
  80. log.Fatalf("did not find any files to add")
  81. }
  82. if !seen["US/Eastern"] {
  83. log.Fatalf("did not find US/Eastern to add")
  84. }
  85. if err := os.WriteFile(args[0], zb.Bytes(), 0666); err != nil {
  86. log.Fatal(err)
  87. }
  88. }