Last active
February 20, 2018 16:29
-
-
Save GauthamBanasandra/72e0fc5f49238c8105947f356b582e6f to your computer and use it in GitHub Desktop.
zipping a file in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function OnUpdate(doc, meta) { | |
var abv = 2, | |
count = 0, | |
lim = 10; | |
var q = SELECT * FROM `beer-sample` | |
WHERE abv >: abv LIMIT: lim; | |
for (var row of q) { | |
var id = meta.id + (++count); | |
sample[id] = row; | |
} | |
} | |
function OnDelete(meta) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"appname": "abv-track", | |
"id": 0, | |
"depcfg": { | |
"buckets": [{ | |
"alias": "sample", | |
"bucket_name": "sample" | |
}], | |
"metadata_bucket": "eventing", | |
"source_bucket": "default" | |
}, | |
"settings": { | |
"app_log_max_files": 10, | |
"app_log_max_size": 10485760, | |
"checkpoint_interval": 10000, | |
"cleanup_timers": false, | |
"cpp_worker_thread_count": 2, | |
"curl_timeout": 500, | |
"dcp_stream_boundary": "from_now", | |
"deadline_timeout": 3, | |
"deployment_status": true, | |
"description": "", | |
"enable_recursive_mutation": false, | |
"execution_timeout": 1, | |
"fuzz_offset": 30, | |
"lcb_inst_capacity": 5, | |
"log_level": "TRACE", | |
"memory_quota": 256, | |
"processing_status": true, | |
"rbacpass": "asdasd", | |
"rbacrole": "admin", | |
"rbacuser": "eventing", | |
"skip_timer_threshold": 86400, | |
"sock_batch_size": 1, | |
"tick_duration": 5000, | |
"timer_processing_tick_interval": 500, | |
"timer_worker_pool_size": 3, | |
"vb_ownership_giveup_routine_count": 3, | |
"vb_ownership_takeover_routine_count": 3, | |
"worker_count": 3, | |
"worker_queue_cap": 1000000, | |
"xattr_doc_timer_entry_prune_threshold": 100 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"path/filepath" | |
"os" | |
"archive/zip" | |
"strings" | |
"fmt" | |
"io" | |
"bytes" | |
) | |
func createFileIfNotExists(path string) error { | |
if _, err := os.Stat(path); err != nil { | |
if os.IsNotExist(err) { | |
_, err = os.Create(path) | |
if err != nil { | |
fmt.Printf("error: %v\n", err) | |
return err | |
} | |
} | |
} | |
return nil | |
} | |
func createDirIfNotExists(path string) error { | |
if _, err := os.Stat(path); err != nil { | |
if os.IsNotExist(err) { | |
err = os.Mkdir(path, os.ModePerm) | |
if err != nil { | |
fmt.Printf("error: %v\n", err) | |
return err | |
} | |
} | |
} | |
return nil | |
} | |
func dummyZip(appName string) { | |
appDir := filepath.Join(os.TempDir(), appName) | |
appCode := filepath.Join(os.TempDir(), appName, appName) + ".js" | |
appConfig := filepath.Join(os.TempDir(), appName, appName) + ".json" | |
zipDir := filepath.Join(os.TempDir(), appName) + ".zip" | |
err := createDirIfNotExists(appDir) | |
if err != nil { | |
fmt.Printf("Unable to create app dir, error: %v", err) | |
return | |
} | |
err = createFileIfNotExists(appConfig) | |
if err != nil { | |
fmt.Printf("Unable to create app config, error: %v", err) | |
return | |
} | |
err = createFileIfNotExists(appCode) | |
if err != nil { | |
fmt.Printf("Unable to create app code, error: %v", err) | |
return | |
} | |
fmt.Printf("source path: %v\nzip path: %v\nappCode path: %v\nappConfig path: %v\n", appDir, zipDir, appCode, appConfig) | |
} | |
func main() { | |
dummyZip("abv-track") | |
return | |
source := `C:\Users\gauth\go\src\zip-file\abv-track` | |
target := `C:\Users\gauth\go\src\zip-file\abv-track.zip` | |
zipFile, err := os.Create(target) | |
if err != nil { | |
fmt.Printf("Unable to create zip file, err : %v", err) | |
return | |
} | |
defer zipFile.Close() | |
archive := zip.NewWriter(zipFile) | |
defer archive.Close() | |
fileInfo, err := os.Stat(source) | |
if err != nil { | |
fmt.Printf("Unable to stat source, err : %v", err) | |
return | |
} | |
var baseDir string | |
if fileInfo.IsDir() { | |
baseDir = filepath.Base(source) | |
} | |
filepath.Walk(source, func(path string, fileInfo os.FileInfo, err error) error { | |
if err != nil { | |
return err | |
} | |
header, err := zip.FileInfoHeader(fileInfo) | |
if err != nil { | |
return err | |
} | |
if baseDir != "" { | |
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source)) | |
} | |
if fileInfo.IsDir() { | |
header.Name += "/" | |
} else { | |
header.Method = zip.Deflate | |
} | |
writer, err := archive.CreateHeader(header) | |
if err != nil { | |
return err | |
} | |
if fileInfo.IsDir() { | |
return nil | |
} | |
fmt.Printf("Zipping file %s\n", path) | |
file, err := os.Open(path) | |
if err != nil { | |
return err | |
} | |
defer file.Close() | |
_, err = io.Copy(writer, bytes.NewReader([]byte("hello there, this isn't from file"))) | |
return err | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment