11package main
22
33import (
4+ "archive/zip"
45 "encoding/json"
56 "errors"
67 "fmt"
@@ -75,7 +76,7 @@ func rmMod(modName string) error {
7576}
7677
7778func createModPackDir () error {
78- err := os .Mkdir (config .FactorioDir + "/ modpacks" , 0775 )
79+ err := os .Mkdir (filepath . Join ( config .FactorioDir , " modpacks") , 0775 )
7980 if err != nil {
8081 log .Printf ("Could not create modpacks directory: %s" , err )
8182 return err
@@ -84,6 +85,53 @@ func createModPackDir() error {
8485 return nil
8586}
8687
88+ // Create's modpack zip file from provided title, mods parameter is a string of mod filenames
89+ func createModPack (title string , mods ... string ) error {
90+ zipfile , err := os .Create (filepath .Join (config .FactorioDir , "modpacks" , title + ".zip" ))
91+ if err != nil {
92+ log .Printf ("Error creating zipfile: %s, error: %s" , title , err )
93+ }
94+ defer zipfile .Close ()
95+ // Create Zip writer
96+ z := zip .NewWriter (zipfile )
97+ defer z .Close ()
98+
99+ for _ , mod := range mods {
100+ // Process mod file, add to zipfile
101+ f , err := os .Open (filepath .Join (config .FactorioDir , "mods" , mod ))
102+ if err != nil {
103+ log .Printf ("Error creating modpack file %s for archival: " , mod , err )
104+ return err
105+ }
106+ // Read contents of mod to be compressed
107+ modfile , err := ioutil .ReadAll (f )
108+ if err != nil {
109+ log .Printf ("Error reading modfile contents: %s" , err )
110+ continue
111+ }
112+ // Add file to zip archive
113+ fmt .Println (mod )
114+ zip , err := z .Create (mod )
115+ if err != nil {
116+ log .Printf ("Error adding file: %s to zip: %s" , f .Name , err )
117+ continue
118+ }
119+ // Write file contents to zip archive
120+ _ , err = zip .Write (modfile )
121+ if err != nil {
122+ log .Printf ("Error writing to zipfile: %s" , err )
123+ continue
124+ }
125+ }
126+
127+ err = z .Close ()
128+ if err != nil {
129+ log .Printf ("Error trying to zip: %s, error: %s" , title , err )
130+ }
131+
132+ return nil
133+ }
134+
87135// Parses mod-list.json file in factorio/mods
88136// returns ModList struct
89137func parseModList () (ModList , error ) {
0 commit comments