Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ _testmain.go
*.exe
*.test
*.prof

vendor/
.idea/
/go.sum
2 changes: 1 addition & 1 deletion crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestPasswordHelloWorldAes(t *testing.T) {
var b bytes.Buffer
for _, f := range r.File {
if !f.IsEncrypted() {
t.Errorf("Expected %s to be encrypted.", f.FileInfo().Name)
t.Errorf("Expected %s to be encrypted.", f.FileInfo().Name())
}
f.SetPassword("golang")
rc, err := f.Open()
Expand Down
34 changes: 34 additions & 0 deletions demo/localtime/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"bytes"
"github.com/26huitailang/zip"
"io"
"log"
"os"
"time"
)

func main() {
contents := []byte("Hello World")
fzip, err := os.Create(`./test.zip`)
if err != nil {
log.Fatalln(err)
}
fh := &zip.FileHeader{
Name: `test.txt`,
Method: zip.Deflate,
}
fh.SetModTime(time.Now(), time.Local)
zipw := zip.NewWriter(fzip)
w, err := zipw.CreateHeader(fh)
if err != nil {
log.Fatal(err)
}
defer zipw.Close()
_, err = io.Copy(w, bytes.NewReader(contents))
if err != nil {
log.Fatal(err)
}
zipw.Flush()
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/26huitailang/zip

go 1.23.2

require (
github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0
golang.org/x/crypto v0.29.0
)
16 changes: 10 additions & 6 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ func (fi headerFileInfo) Sys() interface{} { return fi.fh }
// Because os.FileInfo's Name method returns only the base name of
// the file it describes, it may be necessary to modify the Name field
// of the returned header to provide the full path name of the file.
func FileInfoHeader(fi os.FileInfo) (*FileHeader, error) {
func FileInfoHeader(fi os.FileInfo, loc *time.Location) (*FileHeader, error) {
size := fi.Size()
fh := &FileHeader{
Name: fi.Name(),
UncompressedSize64: uint64(size),
}
fh.SetModTime(fi.ModTime())
fh.SetModTime(fi.ModTime(), loc)
fh.SetMode(fi.Mode())
if fh.UncompressedSize64 > uint32max {
fh.UncompressedSize = uint32max
Expand Down Expand Up @@ -183,8 +183,12 @@ func msDosTimeToTime(dosDate, dosTime uint16) time.Time {
// timeToMsDosTime converts a time.Time to an MS-DOS date and time.
// The resolution is 2s.
// See: http://msdn.microsoft.com/en-us/library/ms724274(v=VS.85).aspx
func timeToMsDosTime(t time.Time) (fDate uint16, fTime uint16) {
t = t.In(time.UTC)
func timeToMsDosTime(t time.Time, loc *time.Location) (fDate uint16, fTime uint16) {
if loc == nil {
t = t.In(time.UTC)
} else {
t = t.In(loc)
}
fDate = uint16(t.Day() + int(t.Month())<<5 + (t.Year()-1980)<<9)
fTime = uint16(t.Second()/2 + t.Minute()<<5 + t.Hour()<<11)
return
Expand All @@ -198,8 +202,8 @@ func (h *FileHeader) ModTime() time.Time {

// SetModTime sets the ModifiedTime and ModifiedDate fields to the given time in UTC.
// The resolution is 2s.
func (h *FileHeader) SetModTime(t time.Time) {
h.ModifiedDate, h.ModifiedTime = timeToMsDosTime(t)
func (h *FileHeader) SetModTime(t time.Time, loc *time.Location) {
h.ModifiedDate, h.ModifiedTime = timeToMsDosTime(t, loc)
}

const (
Expand Down
16 changes: 13 additions & 3 deletions zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ func TestOver65kFiles(t *testing.T) {
func TestModTime(t *testing.T) {
var testTime = time.Date(2009, time.November, 10, 23, 45, 58, 0, time.UTC)
fh := new(FileHeader)
fh.SetModTime(testTime)
fh.SetModTime(testTime, nil)
outTime := fh.ModTime()
if !outTime.Equal(testTime) {
t.Errorf("times don't match: got %s, want %s", outTime, testTime)
}
}

func TestModTimeLocal(t *testing.T) {
var testTime = time.Date(2009, time.November, 10, 23, 45, 58, 0, time.Local)
fh := new(FileHeader)
fh.SetModTime(testTime, nil)
outTime := fh.ModTime()
if !outTime.Equal(testTime) {
t.Errorf("times don't match: got %s, want %s", outTime, testTime)
Expand All @@ -62,7 +72,7 @@ func TestModTime(t *testing.T) {

func testHeaderRoundTrip(fh *FileHeader, wantUncompressedSize uint32, wantUncompressedSize64 uint64, t *testing.T) {
fi := fh.FileInfo()
fh2, err := FileInfoHeader(fi)
fh2, err := FileInfoHeader(fi, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -390,7 +400,7 @@ func TestHeaderInvalidTagAndSize(t *testing.T) {
Method: Deflate,
Extra: []byte(ts.Format(time.RFC3339Nano)), // missing tag and len
}
h.SetModTime(ts)
h.SetModTime(ts, nil)

testInvalidHeader(&h, t)
}
Expand Down