diff --git a/main.go b/main.go index c0cd418..e8e171b 100644 --- a/main.go +++ b/main.go @@ -211,54 +211,13 @@ func (a *Archiver) Close() error { } func (a *Archiver) archive(f *File) error { - err := a.writeFile(f) - + fileWriter, err := a.w.CreateRaw(f.Header) if err != nil { return err } - return nil -} + _, err = io.Copy(fileWriter, &f.CompressedData) -func (a *Archiver) writeFile(f *File) error { - writer, err := a.createFile(f.Info) - if err != nil { - return err - } - - if f.Info.IsDir() { - return nil - } - - file, err := os.Open(f.Path) - if err != nil { - return err - } - - err = a.writeContents(writer, file) - if err != nil { - return err - } - - return nil -} - -func (a *Archiver) createFile(info fs.FileInfo) (io.Writer, error) { - header, err := zip.FileInfoHeader(info) - if err != nil { - return nil, err - } - - writer, err := a.w.CreateHeader(header) - if err != nil { - return nil, err - } - - return writer, nil -} - -func (a *Archiver) writeContents(w io.Writer, r io.Reader) error { - _, err := io.Copy(w, r) if err != nil { return err } @@ -385,82 +344,5 @@ func (a *Archiver) dirArchive() bool { return a.chroot != "" } -type FileHeader struct { - // Name is the name of the file. - // - // It must be a relative path, not start with a drive letter (such as "C:"), - // and must use forward slashes instead of back slashes. A trailing slash - // indicates that this file is a directory and should have no data. - Name string - - // Comment is any arbitrary user-defined string shorter than 64KiB. - Comment string - - // NonUTF8 indicates that Name and Comment are not encoded in UTF-8. - // - // By specification, the only other encoding permitted should be CP-437, - // but historically many ZIP readers interpret Name and Comment as whatever - // the system's local character encoding happens to be. - // - // This flag should only be set if the user intends to encode a non-portable - // ZIP file for a specific localized region. Otherwise, the Writer - // automatically sets the ZIP format's UTF-8 flag for valid UTF-8 strings. - NonUTF8 bool - - CreatorVersion uint16 - ReaderVersion uint16 - Flags uint16 - - // Method is the compression method. If zero, Store is used. - Method uint16 - - // Modified is the modified time of the file. - // - // When reading, an extended timestamp is preferred over the legacy MS-DOS - // date field, and the offset between the times is used as the timezone. - // If only the MS-DOS date is present, the timezone is assumed to be UTC. - // - // When writing, an extended timestamp (which is timezone-agnostic) is - // always emitted. The legacy MS-DOS date field is encoded according to the - // location of the Modified time. - Modified time.Time - - // ModifiedTime is an MS-DOS-encoded time. - // - // Deprecated: Use Modified instead. - ModifiedTime uint16 - - // ModifiedDate is an MS-DOS-encoded date. - // - // Deprecated: Use Modified instead. - ModifiedDate uint16 - - // CRC32 is the CRC32 checksum of the file content. - CRC32 uint32 - - // CompressedSize is the compressed size of the file in bytes. - // If either the uncompressed or compressed size of the file - // does not fit in 32 bits, CompressedSize is set to ^uint32(0). - // - // Deprecated: Use CompressedSize64 instead. - CompressedSize uint32 - - // UncompressedSize is the compressed size of the file in bytes. - // If either the uncompressed or compressed size of the file - // does not fit in 32 bits, CompressedSize is set to ^uint32(0). - // - // Deprecated: Use UncompressedSize64 instead. - UncompressedSize uint32 - - // CompressedSize64 is the compressed size of the file in bytes. - CompressedSize64 uint64 - - // UncompressedSize64 is the uncompressed size of the file in bytes. - UncompressedSize64 uint64 - - Extra []byte - ExternalAttrs uint32 // Meaning depends on CreatorVersion -} - func main() { }