Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 808bd48

Browse files
committed
Fix FilesFromDisk and make options optional
1 parent 0c724c4 commit 808bd48

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,18 @@ $ go get github.com/mholt/archiver/v4
6363

6464
Creating archives can be done entirely without needing a real disk or storage device since all you need is a list of [`File` structs](https://pkg.go.dev/github.com/mholt/archiver/v4#File) to pass in.
6565

66-
However, creating archives from files on disk is very common, so you can use the `FilesFromDisk()` function to help you map filenames on disk to their paths in the archive. Then create and customize the format type.
66+
However, creating archives from files on disk is very common, so you can use the [`FilesFromDisk()` function](https://pkg.go.dev/github.com/mholt/archiver/v4#FilesFromDisk) to help you map filenames on disk to their paths in the archive. Then create and customize the format type.
6767

68-
In this example, we add 2 files and a directory (which includes its contents recursively) to a .tar.gz file:
68+
In this example, we add 4 files and a directory (which includes its contents recursively) to a .tar.gz file:
6969

7070
```go
7171
// map files on disk to their paths in the archive
72-
files, err := archiver.FilesFromDisk(map[string]string{
72+
files, err := archiver.FilesFromDisk(nil, map[string]string{
7373
"/path/on/disk/file1.txt": "file1.txt",
7474
"/path/on/disk/file2.txt": "subfolder/file2.txt",
75-
"/path/on/disk/folder": "",
75+
"/path/on/disk/file3.txt": "", // put in root of archive as file3.txt
76+
"/path/on/disk/file4.txt": "subfolder/", // put in subfolder as file4.txt
77+
"/path/on/disk/folder": "Custom Folder", // contents added recursively
7678
})
7779
if err != nil {
7880
return err
@@ -99,6 +101,8 @@ if err != nil {
99101
}
100102
```
101103

104+
The first parameter to `FilesFromDisk()` is an optional options struct, allowing you to customize how files are added.
105+
102106
### Extract archive
103107

104108
Extracting an archive, extracting _from_ an archive, and walking an archive are all the same function.

archiver.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,23 @@ func (f File) Stat() (fs.FileInfo, error) { return f.FileInfo, nil }
4545
// given filenames map. The keys are the names on disk, and the values are
4646
// their associated names in the archive. For convenience, empty values are
4747
// interpreted as the base name of the file (sans path) in the root of the
48-
// archive. Keys that specify directories on disk will be walked and added
49-
// to the archive recursively, rooted at the named directory.
48+
// archive; and values that end in a slash will use the bae name of the file
49+
// in that folder of the archive. Keys that specify directories on disk will
50+
// be walked and added to the archive recursively, rooted at the named
51+
// directory.
5052
//
5153
// File gathering will adhere to the settings specified in options.
5254
//
5355
// This function is primarily used when preparing a list of files to add to
5456
// an archive.
55-
func FilesFromDisk(options FromDiskOptions, filenames map[string]string) ([]File, error) {
57+
func FilesFromDisk(options *FromDiskOptions, filenames map[string]string) ([]File, error) {
5658
var files []File
5759
for rootOnDisk, rootInArchive := range filenames {
5860
if rootInArchive == "" {
59-
rootInArchive = filepath.Base(rootInArchive)
61+
rootInArchive = filepath.Base(rootOnDisk)
62+
}
63+
if strings.HasSuffix(rootInArchive, "/") {
64+
rootInArchive += filepath.Base(rootOnDisk)
6065
}
6166

6267
filepath.WalkDir(rootOnDisk, func(filename string, d fs.DirEntry, err error) error {
@@ -73,7 +78,7 @@ func FilesFromDisk(options FromDiskOptions, filenames map[string]string) ([]File
7378
var linkTarget string
7479

7580
if isSymlink(info) {
76-
if options.FollowSymlinks {
81+
if options != nil && options.FollowSymlinks {
7782
// dereference symlinks
7883
filename, err = os.Readlink(filename)
7984
if err != nil {

0 commit comments

Comments
 (0)