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
3 changes: 2 additions & 1 deletion 7z.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"io/fs"
"log"
"path/filepath"
"strings"

"github.com/bodgit/sevenzip"
Expand Down Expand Up @@ -37,7 +38,7 @@ func (z SevenZip) Match(_ context.Context, filename string, stream io.Reader) (M
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), z.Extension()) {
if filepath.Ext(strings.ToLower(filename)) == z.Extension() {
mr.ByName = true
}

Expand Down
3 changes: 2 additions & 1 deletion brotli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io"
"path/filepath"
"strings"
"unicode/utf8"

Expand All @@ -26,7 +27,7 @@ func (br Brotli) Match(ctx context.Context, filename string, stream io.Reader) (
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), br.Extension()) {
if filepath.Ext(strings.ToLower(filename)) == br.Extension() {
mr.ByName = true
}

Expand Down
3 changes: 2 additions & 1 deletion bz2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io"
"path/filepath"
"strings"

"github.com/dsnet/compress/bzip2"
Expand All @@ -25,7 +26,7 @@ func (bz Bz2) Match(_ context.Context, filename string, stream io.Reader) (Match
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), bz.Extension()) {
if filepath.Ext(strings.ToLower(filename)) == bz.Extension() {
mr.ByName = true
}

Expand Down
38 changes: 38 additions & 0 deletions formats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,44 @@ func checkErr(t *testing.T, err error, msgFmt string, args ...any) {
t.Fatalf(msgFmt+": %s", args...)
}

func TestIdentifyFindFormatByFileName(t *testing.T) {
tests := []struct {
filename string
expected string
}{
{
filename: "test.tar",
expected: ".tar",
},
{
filename: "test.tar.bz2",
expected: ".tar.bz2",
},
{
filename: "test.tar.br",
expected: ".tar.br",
},
{
filename: "test.tar.bru",
expected: ".tar",
},
{
filename: "test.7z",
expected: ".7z",
},
}

for _, tt := range tests {
t.Run(tt.filename, func(t *testing.T) {
format, _, err := Identify(context.Background(), tt.filename, nil)
checkErr(t, err, "identifying")
if format.Extension() != tt.expected {
t.Errorf("unexpected extension: %v, expected: %v", format.Extension(), tt.expected)
}
})
}
}

func TestIdentifyDoesNotMatchContentFromTrimmedKnownHeaderHaving0Suffix(t *testing.T) {
// Using the outcome of `n, err := io.ReadFull(stream, buf)` without minding n
// may lead to a mis-characterization for cases with known header ending with 0x0
Expand Down
3 changes: 2 additions & 1 deletion gz.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io"
"path/filepath"
"strings"

"github.com/klauspost/compress/gzip"
Expand Down Expand Up @@ -37,7 +38,7 @@ func (gz Gz) Match(_ context.Context, filename string, stream io.Reader) (MatchR
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), gz.Extension()) {
if filepath.Ext(strings.ToLower(filename)) == gz.Extension() {
mr.ByName = true
}

Expand Down
3 changes: 2 additions & 1 deletion lz4.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io"
"path/filepath"
"strings"

"github.com/pierrec/lz4/v4"
Expand All @@ -25,7 +26,7 @@ func (lz Lz4) Match(_ context.Context, filename string, stream io.Reader) (Match
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), lz.Extension()) {
if filepath.Ext(strings.ToLower(filename)) == lz.Extension() {
mr.ByName = true
}

Expand Down
2 changes: 1 addition & 1 deletion minlz.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (mz MinLZ) Match(_ context.Context, filename string, stream io.Reader) (Mat
var mr MatchResult

// match filename
if filepath.Ext(strings.ToLower(filename)) == ".mz" {
if filepath.Ext(strings.ToLower(filename)) == mz.Extension() {
mr.ByName = true
}

Expand Down
3 changes: 2 additions & 1 deletion rar.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"os"
"path"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -60,7 +61,7 @@ func (r Rar) Match(_ context.Context, filename string, stream io.Reader) (MatchR
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), r.Extension()) {
if filepath.Ext(strings.ToLower(filename)) == r.Extension() {
mr.ByName = true
}

Expand Down
5 changes: 3 additions & 2 deletions sz.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io"
"path/filepath"
"strings"

"github.com/klauspost/compress/s2"
Expand Down Expand Up @@ -51,8 +52,8 @@ func (sz Sz) Match(_ context.Context, filename string, stream io.Reader) (MatchR
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), sz.Extension()) ||
strings.Contains(strings.ToLower(filename), ".s2") {
if filepath.Ext(strings.ToLower(filename)) == sz.Extension() ||
filepath.Ext(strings.ToLower(filename)) == ".s2" {
mr.ByName = true
}

Expand Down
3 changes: 2 additions & 1 deletion xz.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io"
"path/filepath"
"strings"

fastxz "github.com/mikelolasagasti/xz"
Expand All @@ -24,7 +25,7 @@ func (x Xz) Match(_ context.Context, filename string, stream io.Reader) (MatchRe
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), x.Extension()) {
if filepath.Ext(strings.ToLower(filename)) == x.Extension() {
mr.ByName = true
}

Expand Down
3 changes: 2 additions & 1 deletion zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"os"
"path"
"path/filepath"
"strings"

szip "github.com/STARRY-S/zip"
Expand Down Expand Up @@ -85,7 +86,7 @@ func (z Zip) Match(_ context.Context, filename string, stream io.Reader) (MatchR
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), z.Extension()) {
if filepath.Ext(strings.ToLower(filename)) == z.Extension() {
mr.ByName = true
}

Expand Down
3 changes: 2 additions & 1 deletion zlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package archives
import (
"context"
"io"
"path/filepath"
"strings"

"github.com/klauspost/compress/zlib"
Expand All @@ -24,7 +25,7 @@ func (zz Zlib) Match(_ context.Context, filename string, stream io.Reader) (Matc
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), zz.Extension()) {
if filepath.Ext(strings.ToLower(filename)) == zz.Extension() {
mr.ByName = true
}

Expand Down
3 changes: 2 additions & 1 deletion zstd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io"
"path/filepath"
"strings"

"github.com/klauspost/compress/zstd"
Expand All @@ -26,7 +27,7 @@ func (zs Zstd) Match(_ context.Context, filename string, stream io.Reader) (Matc
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), zs.Extension()) {
if filepath.Ext(strings.ToLower(filename)) == zs.Extension() {
mr.ByName = true
}

Expand Down
Loading