@@ -76,44 +76,58 @@ func compress(projectPath string, filepaths []string, output io.Writer) (err err
7676 }()
7777
7878 for _ , path := range filepaths {
79- // path variables only contains relative path of the project root, so we need to append project path to get absolute path.
80- // path/filepath packages works cross platform which will use appropriate file separator based on OS.
81- fullpath := filepath .Join (projectPath , path )
82-
83- file , err := os .Open (fullpath )
79+ isDir , err := compressFile (projectPath , path , tarWriter )
8480 if err != nil {
8581 return err
8682 }
87- defer func () {
88- closeErr := file .Close ()
89- if err == nil {
90- err = closeErr
91- }
92- }()
93-
94- fileInfo , err := file .Stat ()
95- if err != nil {
96- return err
97- }
98- if fileInfo .IsDir () {
83+ if isDir {
9984 continue
10085 }
86+ }
10187
102- header , err := tar .FileInfoHeader (fileInfo , fileInfo .Name ())
103- if err != nil {
104- return err
105- }
106- header .Name = strings .Replace (strings .TrimPrefix (path , string (filepath .Separator )), "\\ " , "/" , - 1 )
107- err = tarWriter .WriteHeader (header )
108- if err != nil {
109- return err
110- }
88+ return nil
89+ }
11190
112- _ , err = io .Copy (tarWriter , file )
113- if err != nil {
114- return err
91+ func compressFile (projectPath , path string , writer * tar.Writer ) (bool , error ) {
92+ // path variables only contains relative path of the project root, so we need to append project path to get absolute path.
93+ // path/filepath packages works cross platform which will use appropriate file separator based on OS.
94+ fullpath := filepath .Join (projectPath , path )
95+
96+ file , err := os .Open (fullpath )
97+ if err != nil {
98+ return false , err
99+ }
100+
101+ defer func () {
102+ closeErr := file .Close ()
103+ if err == nil {
104+ err = closeErr
115105 }
106+ }()
107+
108+ fileInfo , err := file .Stat ()
109+ if err != nil {
110+ return false , err
111+ }
112+ if fileInfo .IsDir () {
113+ return true , nil
116114 }
117115
118- return nil
116+ header , err := tar .FileInfoHeader (fileInfo , fileInfo .Name ())
117+ if err != nil {
118+ return false , err
119+ }
120+
121+ header .Name = strings .Replace (strings .TrimPrefix (path , string (filepath .Separator )), "\\ " , "/" , - 1 )
122+ err = writer .WriteHeader (header )
123+ if err != nil {
124+ return false , err
125+ }
126+
127+ _ , err = io .Copy (writer , file )
128+ if err != nil {
129+ return false , err
130+ }
131+
132+ return false , nil
119133}
0 commit comments