From 09bbccc36c86ca5404a4be1c15639c363151dbfa Mon Sep 17 00:00:00 2001 From: halfcrazy Date: Wed, 13 Dec 2023 11:06:09 +0800 Subject: [PATCH] Fix create archive to a continuous writing source file failed (#388) * Fix create archive to a continuous writing source file failed https://github.com/mholt/archiver/issues/387 Signed-off-by: Yan Zhu * Update archiver.go --------- Signed-off-by: Yan Zhu Co-authored-by: Matt Holt --- archiver.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/archiver.go b/archiver.go index 73ec00d4..1701b078 100644 --- a/archiver.go +++ b/archiver.go @@ -221,8 +221,14 @@ func openAndCopyFile(file File, w io.Writer) error { return err } defer fileReader.Close() - _, err = io.Copy(w, fileReader) - return err + // When file is in use and size is being written to, creating the compressed + // file will fail with "archive/tar: write too long." Using CopyN gracefully + // handles this. + _, err = io.CopyN(w, fileReader, file.Size()) + if err != nil && err != io.EOF { + return err + } + return nil } // fileIsIncluded returns true if filename is included according to