Skip to content

Commit 90577ec

Browse files
authored
Merge pull request #50 from michalc/symlink-not-use-data-descriptor
Don't use a data descriptor for symlinks so they can be stream-unzipped
2 parents 7b15cf5 + fd1b38a commit 90577ec

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

archiver.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"context"
66
"fmt"
7+
"hash/crc32"
78
"io"
89
"os"
910
"path/filepath"
@@ -228,12 +229,19 @@ func (a *Archiver) createSymlink(path string, fi os.FileInfo, hdr *zip.FileHeade
228229
a.m.Lock()
229230
defer a.m.Unlock()
230231

231-
w, err := a.createHeader(fi, hdr)
232+
link, err := os.Readlink(path)
232233
if err != nil {
233234
return err
234235
}
235236

236-
link, err := os.Readlink(path)
237+
// Don't use a data descriptor to shave a few bytes and to make sure that the symlink can be stream-unzipped
238+
hdr.Flags &= ^uint16(0x8)
239+
hdr.Method = zip.Store
240+
hdr.CompressedSize64 = uint64(len(link))
241+
hdr.UncompressedSize64 = hdr.CompressedSize64
242+
hdr.CRC32 = crc32.ChecksumIEEE([]byte(link))
243+
244+
w, err := a.createHeaderRaw(fi, hdr)
237245
if err != nil {
238246
return err
239247
}
@@ -282,6 +290,7 @@ func (a *Archiver) compressFile(ctx context.Context, f *os.File, fi os.FileInfo,
282290
return err
283291
}
284292

293+
hdr.Flags |= 0x8
285294
hdr.CompressedSize64 = tmp.Written()
286295
// if compressed file is larger, use the uncompressed version.
287296
if hdr.CompressedSize64 > hdr.UncompressedSize64 {
@@ -349,8 +358,6 @@ func (a *Archiver) createHeaderRaw(fi os.FileInfo, fh *zip.FileHeader) (io.Write
349358
fh.Extra = append(fh.Extra, zipextra.NewExtendedTimestamp(fh.Modified).Encode()...)
350359
}
351360

352-
fh.Flags |= 0x8
353-
354361
return a.createRaw(fi, fh)
355362
}
356363

0 commit comments

Comments
 (0)