Пример #1
0
func NewTPExporter(storDb LoadStorage, tpID, expPath, fileFormat, sep string, compress bool) (*TPExporter, error) {
	if len(tpID) == 0 {
		return nil, errors.New("Missing TPid")
	}
	if !utils.IsSliceMember(TPExportFormats, fileFormat) {
		return nil, errors.New("Unsupported file format")
	}
	tpExp := &TPExporter{
		storDb:     storDb,
		tpID:       tpID,
		exportPath: expPath,
		fileFormat: fileFormat,
		compress:   compress,
		cacheBuff:  new(bytes.Buffer),
	}
	runeSep, _ := utf8.DecodeRuneInString(sep)
	if runeSep == utf8.RuneError {
		return nil, fmt.Errorf("Invalid field separator: %s", sep)
	} else {
		tpExp.sep = runeSep
	}
	if compress {
		if len(tpExp.exportPath) == 0 {
			tpExp.zipWritter = zip.NewWriter(tpExp.cacheBuff)
		} else {
			if fileOut, err := os.Create(path.Join(tpExp.exportPath, "tpexport.zip")); err != nil {
				return nil, err
			} else {
				tpExp.zipWritter = zip.NewWriter(fileOut)
			}
		}
	}
	return tpExp, nil
}
Пример #2
0
func zipIt(files []IncludedFile) string {
	buf := new(bytes.Buffer)
	zipWriter := zip.NewWriter(buf)

	for _, file := range files {
		zipFile, err := zipWriter.Create(file.Name)
		if err != nil {
			check(err)
		}
		_, err = zipFile.Write([]byte(file.Body))
		if err != nil {
			check(err)
		}
	}

	err := zipWriter.Close()
	check(err)

	// Write zipfile to disk.
	t := time.Now()
	finalName := fmt.Sprintf("graylog_apollo_bundle-%d-%02d-%02dT%02d-%02d-%02d.zip", t.Year(), t.Month(), t.Day(),
		t.Hour(), t.Minute(), t.Second())
	ioutil.WriteFile(finalName, buf.Bytes(), 0644)

	return finalName
}
Пример #3
0
func writeZipFile(dir string, targetFile *os.File) (err error) {
	isEmpty, err := fileutils.IsDirEmpty(dir)
	if err != nil {
		return
	}
	if isEmpty {
		err = errors.New("Directory is empty")
		return
	}

	writer := zip.NewWriter(targetFile)
	defer writer.Close()

	err = WalkAppFiles(dir, func(fileName string, fullPath string) (err error) {
		fileInfo, err := os.Stat(fullPath)
		if err != nil {
			return err
		}

		header, err := zip.FileInfoHeader(fileInfo)
		header.Name = filepath.ToSlash(fileName)
		if err != nil {
			return err
		}

		zipFilePart, err := writer.CreateHeader(header)
		err = fileutils.CopyPathToWriter(fullPath, zipFilePart)
		return
	})

	return
}
Пример #4
0
// ExportKeysByGUN exports all keys associated with a specified GUN to an
// io.Writer in zip format. passphraseRetriever is used to select new passphrases to use to
// encrypt the keys.
func (km *KeyStoreManager) ExportKeysByGUN(dest io.Writer, gun string, passphraseRetriever passphrase.Retriever) error {
	tempBaseDir, err := ioutil.TempDir("", "notary-key-export-")
	defer os.RemoveAll(tempBaseDir)

	privNonRootKeysSubdir := filepath.Join(privDir, nonRootKeysSubdir)

	// Create temporary keystore to use as a staging area
	tempNonRootKeysPath := filepath.Join(tempBaseDir, privNonRootKeysSubdir)
	tempNonRootKeyStore, err := trustmanager.NewKeyFileStore(tempNonRootKeysPath, passphraseRetriever)
	if err != nil {
		return err
	}

	if err := moveKeysByGUN(km.nonRootKeyStore, tempNonRootKeyStore, gun); err != nil {
		return err
	}

	zipWriter := zip.NewWriter(dest)

	if len(tempNonRootKeyStore.ListKeys()) == 0 {
		return ErrNoKeysFoundForGUN
	}

	if err := addKeysToArchive(zipWriter, tempNonRootKeyStore, privNonRootKeysSubdir); err != nil {
		return err
	}

	zipWriter.Close()

	return nil
}
Пример #5
0
// Just zip up what ever is in the path
func zipResource(path string, topLevelFolder string) {
	zipfile := new(bytes.Buffer)
	zipper := zip.NewWriter(zipfile)
	startPath := path + "/"
	filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
		if filepath.Base(path) != ".DS_Store" {
			// Can skip dirs since the dirs will be created when the files are added
			if !f.IsDir() {
				file, err := ioutil.ReadFile(path)
				if err != nil {
					return err
				}
				fl, err := zipper.Create(filepath.Join(topLevelFolder, strings.Replace(path, startPath, "", -1)))
				if err != nil {
					ErrorAndExit(err.Error())
				}
				_, err = fl.Write([]byte(file))
				if err != nil {
					ErrorAndExit(err.Error())
				}
			}
		}
		return nil
	})

	zipper.Close()
	zipdata := zipfile.Bytes()
	ioutil.WriteFile(path+".resource", zipdata, 0644)
	return
}
Пример #6
0
func ExampleWriter() {
	// Create a buffer to write our archive to.
	buf := new(bytes.Buffer)

	// Create a new zip archive.
	w := zip.NewWriter(buf)

	// Add some files to the archive.
	var files = []struct {
		Name, Body string
	}{
		{"readme.txt", "This archive contains some text files."},
		{"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
		{"todo.txt", "Get animal handling licence.\nWrite more examples."},
	}
	for _, file := range files {
		f, err := w.Create(file.Name)
		if err != nil {
			log.Fatal(err)
		}
		_, err = f.Write([]byte(file.Body))
		if err != nil {
			log.Fatal(err)
		}
	}

	// Make sure to check the error on Close.
	err := w.Close()
	Strtofile(buf.Bytes(), `c:\xx.zip`)
	if err != nil {
		log.Fatal(err)
	}
}
Пример #7
0
// Save the File to an xlsx file at the provided path.
func (f *File) Save(path string) (err error) {
	var parts map[string]string
	var target *os.File
	var zipWriter *zip.Writer

	parts, err = f.MarshallParts()
	if err != nil {
		return
	}

	target, err = os.Create(path)
	if err != nil {
		return
	}

	zipWriter = zip.NewWriter(target)

	for partName, part := range parts {
		var writer io.Writer
		writer, err = zipWriter.Create(partName)
		if err != nil {
			return
		}
		_, err = writer.Write([]byte(part))
		if err != nil {
			return
		}
	}
	err = zipWriter.Close()
	if err != nil {
		return
	}

	return target.Close()
}
Пример #8
0
func (z *zipper) Archive(src, dest string) error {
	if err := os.MkdirAll(filepath.Dir(dest), 0777); err != nil {
		return err
	}
	out, err := os.Create(dest)
	if err != nil {
		return err
	}
	defer out.Close()
	w := zip.NewWriter(out)
	defer w.Close()
	return filepath.Walk(src, func(path string, info os.FileInfo,
		err error) error {
		if info.IsDir() {
			return nil // スキップします
		}
		if err != nil {
			return err
		}
		in, err := os.Open(path)
		if err != nil {
			return err
		}
		defer in.Close()
		f, err := w.Create(path)
		if err != nil {
			return err
		}
		io.Copy(f, in)
		return nil
	})
}
Пример #9
0
func getZip(e env, projectId string) bool {
	userKey, done := getUser(e)
	if done {
		return true
	}

	snapshot, snapshotKey, done := getLastSnapshot(e, userKey, projectId)
	if done {
		return true
	}

	e.w.Header().Set("Content-Type", "application/zip")
	e.w.Header().Set("Content-Disposition", "inline; filename="+projectId+".zip")

	w := zip.NewWriter(e.w)
	done = addZipFile(e, w, "", snapshot.Top, snapshotKey)
	if done {
		return true
	}

	err := w.Close()
	if err != nil {
		e.w.Header().Set("Content-type", "text/html; charset=utf-8")
		e.w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(e.w, `Could not wrap up the zip file`)
		log.Println("Error finishing zip file", err)
		return true
	}

	return true
}
Пример #10
0
// Función encargada de crear un archivo ZIP y escribir sobre él el contenido
// de un directorio/archivo
func Zip(startPath, finalFileName, finalFilePath string) error {
	// Abrimos la ruta inicial
	startDirectory, err := openPath(startPath)
	if err != nil {
		return err
	}
	// Cerramos la ruta inicial
	defer startDirectory.Close()

	// Creamos directorio destino
	if err := createDirectory(finalFilePath, 0777); err != nil {
		return err
	}

	// Creamos archivo ZIP
	zipFile, err := createFile(finalFilePath + finalFileName)
	if err != nil {
		return err
	}
	// Cerramos archivo ZIP
	defer zipFile.Close()

	// Creamos writer para el archivo ZIP
	zipWriter := zip.NewWriter(zipFile)
	// Cerramos Writer
	defer zipWriter.Close()

	// Iniciamos recorrido del directorio
	if err := walkDirectory(startDirectory, zipWriter); err != nil {
		return err
	}
	return nil
}
Пример #11
0
// WriteToFile creates a zip with all its contents
func (d *Docx) WriteToFile(target string) error {
	docxFile, err := os.Create(target)
	if err != nil {
		return err
	}
	defer docxFile.Close()

	d.Writer = zip.NewWriter(docxFile)
	defer d.Writer.Close()

	for _, f := range d.Files {
		zippedFile, err := d.Writer.Create(f.Name)
		if err != nil {
			return err
		}
		zippedFile.Write(f.Contents)
	}

	zippedFile, err := d.Writer.Create("word/document.xml")
	if err != nil {
		return err
	}
	zippedFile.Write([]byte(d.WordXML))

	return nil
}
Пример #12
0
func nwBuild(nw string) error {
	w, err := os.Create(nw)
	if err != nil {
		return err
	}
	defer w.Close()

	zw := zip.NewWriter(w)
	defer zw.Close()

	r, err := os.Open(app)
	if err != nil {
		return err
	}
	defer r.Close()

	bin := filepath.Base(app)
	p := build.Package{Name: name, Bin: bin, Window: build.Window{Title: name, Toolbar: toolbar, Fullscreen: fullscreen, Frame: frame}}

	if err := p.CreateNW(zw, build.DefaultTemplates, r, includesDir); err != nil {
		return err
	}

	return nil
}
Пример #13
0
func CreateZipArchive(filename string, files []ArchiveFile) {
	file, err := os.Create(filename)
	Ω(err).ShouldNot(HaveOccurred())

	w := zip.NewWriter(file)

	for _, file := range files {
		header := &zip.FileHeader{
			Name: file.Name,
		}

		mode := file.Mode
		if mode == 0 {
			mode = 0777
		}

		header.SetMode(os.FileMode(mode))

		f, err := w.CreateHeader(header)
		Ω(err).ShouldNot(HaveOccurred())

		_, err = f.Write([]byte(file.Body))
		Ω(err).ShouldNot(HaveOccurred())
	}

	err = w.Close()
	Ω(err).ShouldNot(HaveOccurred())

	err = file.Close()
	Ω(err).ShouldNot(HaveOccurred())
}
Пример #14
0
func CreateTestZip(t *T) *bytes.Reader {
	t.Log("Creating in-memory zip file")
	buf := new(bytes.Buffer)
	zw := zip.NewWriter(buf)
	for _, file := range files {
		t.Logf("Adding file %q (%d bytes) to zip file", file.Path, len(file.Contents))

		fw, err := zw.Create(file.Path)
		if err != nil {
			t.Fatal(err)
		}
		if _, err := fw.Write(file.Contents); err != nil {
			t.Fatal(err)
		}
	}

	t.Logf("Creating Manifest in zip file")
	if fw, err := zw.Create("MANIFEST"); err != nil {
		t.Fatal(err)
	} else {
		for _, file := range files {
			mimestring := http.DetectContentType(file.Contents)
			mediatype, params, err := mime.ParseMediaType(mimestring)
			if err != nil {
				t.Fatal(err)
			}
			fmt.Fprintf(fw, "%s (%d bytes): %s %v\n", file.Path, len(file.Contents), mediatype, params)
		}
	}
	if err := zw.Close(); err != nil {
		t.Fatal(err)
	}
	t.Log("Finished zip file,", buf.Len(), "bytes written.")
	return bytes.NewReader(buf.Bytes())
}
Пример #15
0
//export Zip
func Zip(zipname *C.char, filenames []*C.char) int32 {
	f, err := os.Create(C.GoString(zipname))
	if err != nil {
		log.Println(err)
		return -1
	}
	defer f.Close()

	w := zip.NewWriter(f)
	defer w.Close()

	for _, name := range filenames {
		ww, err := w.Create(C.GoString(name))
		if err != nil {
			log.Println(err)
			return -1
		}

		fi, err := os.Open(C.GoString(name))
		if err != nil {
			log.Println(err)
			return -1
		}
		defer fi.Close()

		if _, err := io.Copy(ww, fi); err != nil {
			log.Println(err)
			return -1
		}
	}

	return 0
}
Пример #16
0
func _zipbagWalk(input string) (*bytes.Buffer, error) {
	archive := &bytes.Buffer{}
	zipWriter := zip.NewWriter(archive)

	if input != "" {
		err := walk("", input, zipWriter)
		if err != nil {
			return nil, err
		}
	}

	err := zipWriter.Close()
	if err != nil {
		return nil, err
	}

	var data bytes.Buffer
	encoder := &byteWriter{Writer: &data}
	_, err = io.Copy(encoder, archive)
	if err != nil {
		return nil, err
	}

	return &data, nil
}
Пример #17
0
// zip file
func Zipit(source, target string) error {
	zipfile, err := os.Create(target)
	if err != nil {
		return err
	}
	defer zipfile.Close()

	archive := zip.NewWriter(zipfile)
	defer archive.Close()

	info, err := os.Stat(source)
	if err != nil {
		return nil
	}

	var baseDir string
	if info.IsDir() {
		baseDir = filepath.Base(source)
	}

	filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		header, err := zip.FileInfoHeader(info)
		if err != nil {
			return err
		}

		if baseDir != "" {
			header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
		}

		if info.IsDir() {
			header.Name += "/"
		} else {
			header.Method = zip.Deflate
		}

		writer, err := archive.CreateHeader(header)
		if err != nil {
			return err
		}

		if info.IsDir() {
			return nil
		}

		file, err := os.Open(path)
		if err != nil {
			return err
		}
		defer file.Close()
		_, err = io.Copy(writer, file)
		return err
	})

	return err
}
// Zips up the keys in the old repo, and assert that we can import it and use
// said keys.  The 0.1 exported format is just a zip file of all the keys
func TestImport0Dot1Zip(t *testing.T) {
	ks, ret, gun := get0Dot1(t)

	zipFile, err := ioutil.TempFile("", "notary-test-zipFile")
	defer os.RemoveAll(zipFile.Name())
	zipWriter := zip.NewWriter(zipFile)
	assert.NoError(t, err)
	assert.NoError(t, addKeysToArchive(zipWriter, ks))
	zipWriter.Close()
	zipFile.Close()

	origKeys := ks.ListKeys()
	assert.Len(t, origKeys, 3)

	// now import the zip file into a new cryptoservice

	tempDir, err := ioutil.TempDir("", "notary-test-import")
	defer os.RemoveAll(tempDir)
	assert.NoError(t, err)

	ks, err = trustmanager.NewKeyFileStore(tempDir, ret)
	assert.NoError(t, err)
	cs := NewCryptoService(gun, ks)

	zipReader, err := zip.OpenReader(zipFile.Name())
	assert.NoError(t, err)
	defer zipReader.Close()

	assert.NoError(t, cs.ImportKeysZip(zipReader.Reader))
	assertHasKeys(t, cs, origKeys)
}
Пример #19
0
// BundleTo creates a charm file from the charm expanded in dir.
// By convention a charm bundle should have a ".charm" suffix.
func (dir *Dir) BundleTo(w io.Writer) (err error) {
	zipw := zip.NewWriter(w)
	defer zipw.Close()
	zp := zipPacker{zipw, dir.Path, dir.Meta().Hooks()}
	zp.AddRevision(dir.revision)
	return filepath.Walk(dir.Path, zp.WalkFunc())
}
Пример #20
0
func saveOpeners(fn string, openers []opener) (err error) {
	fh, err := os.Create(fn)
	if err != nil {
		return fmt.Errorf("Create %q: %v", fn, err)
	}
	defer func() {
		if e := fh.Close(); e != nil && err == nil {
			err = fmt.Errorf("Close %q: %v", fh.Name(), e)
		}
	}()
	zw := zip.NewWriter(fh)
	for i, o := range openers {
		src, err := o.Open()
		if err != nil {
			return fmt.Errorf("Open %s: %v", o, err)
		}
		zn := fmt.Sprintf("%03d.png", i)
		w, err := zw.Create(zn)
		if err != nil {
			_ = src.Close()
			return fmt.Errorf("Create %q: %v", zn, err)
		}
		_, err = io.Copy(w, src)
		_ = src.Close()
		if err != nil {
			return fmt.Errorf("Close %q: %v", zn, err)
		}
	}
	if err = zw.Close(); err != nil {
		return fmt.Errorf("Close %v: %v", zw, err)
	}
	return nil
}
Пример #21
0
// Write the File to io.Writer as xlsx
func (f *File) Write(writer io.Writer) (err error) {
	var parts map[string]string
	var zipWriter *zip.Writer

	parts, err = f.MarshallParts()
	if err != nil {
		return
	}

	zipWriter = zip.NewWriter(writer)

	for partName, part := range parts {
		var writer io.Writer
		writer, err = zipWriter.Create(partName)
		if err != nil {
			return
		}
		_, err = writer.Write([]byte(part))
		if err != nil {
			return
		}
	}

	err = zipWriter.Close()

	return
}
Пример #22
0
func createZipArchive(files []string, output io.WriteCloser) error {
	archive := zip.NewWriter(output)
	defer archive.Close()

	for _, path := range files {
		path = filepath.ToSlash(path)

		source, err := os.Open(path)
		if err != nil {
			return fmt.Errorf("Unable to read file %s: %s", path, err)
		}
		defer source.Close()

		target, err := archive.Create(path)
		if err != nil {
			return fmt.Errorf("Failed to add zip header for %s: %s", path, err)
		}

		_, err = io.Copy(target, source)
		if err != nil {
			return fmt.Errorf("Failed to copy %s data to archive: %s", path, err)
		}
	}
	return nil
}
Пример #23
0
func mustZipDir(destFilename, srcDir string) string {
	zipFile, err := os.Create(destFilename)
	panicOnError(err, "Failed to create zip file")

	w := zip.NewWriter(zipFile)
	filepath.Walk(srcDir, func(srcPath string, info os.FileInfo, err error) error {
		// Ignore directories (they are represented by the path of written entries).
		if info.IsDir() {
			return nil
		}

		relSrcPath := strings.TrimLeft(srcPath[len(srcDir):], string(os.PathSeparator))

		f, err := w.Create(relSrcPath)
		panicOnError(err, "Failed to create zip entry")

		srcFile, err := os.Open(srcPath)
		panicOnError(err, "Failed to read source file")

		_, err = io.Copy(f, srcFile)
		panicOnError(err, "Failed to copy")

		return nil
	})

	err = w.Close()
	panicOnError(err, "Failed to close archive")

	err = zipFile.Close()
	panicOnError(err, "Failed to close zip file")

	return zipFile.Name()
}
Пример #24
0
//Creates a new zip file to test the dump function
func createZipFile(t *testing.T) []byte {
	// Create a buffer to write our archive to.
	buf := new(bytes.Buffer)

	// Create a new zip archive.
	w := zip.NewWriter(buf)

	// Add some files to the archive.
	for _, file := range files {
		f, err := w.Create(file.Name)
		if err != nil {
			t.Errorf("Unexpected error %v", err)
		}
		_, err = f.Write([]byte(file.Body))
		if err != nil {
			t.Errorf("Unexpected error %v", err)
		}
	}

	// Make sure to check the error on Close.
	err := w.Close()
	if err != nil {
		t.Errorf("Unexpected error %v", err)
	}
	return buf.Bytes()
}
func testAccCreateZipFromFiles(files map[string]string, zipFile *os.File) error {
	zipFile.Truncate(0)
	zipFile.Seek(0, 0)

	w := zip.NewWriter(zipFile)

	for source, destination := range files {
		f, err := w.Create(destination)
		if err != nil {
			return err
		}

		fileContent, err := ioutil.ReadFile(source)
		if err != nil {
			return err
		}

		_, err = f.Write(fileContent)
		if err != nil {
			return err
		}
	}

	err := w.Close()
	if err != nil {
		return err
	}

	return w.Flush()
}
Пример #26
0
func Create(filename string) (*ZipFile, error) {
	file, err := os.Create(filename)
	if err != nil {
		return nil, err
	}
	return &ZipFile{writer: zip.NewWriter(file)}, nil
}
Пример #27
0
//write raw data into a io.Writer
//data key means: rowIndex,columnIndex,value
//Array2XlsxIo can be used to write xlsx file in memory without touch filesystem.
func Array2XlsxIo(data [][]string, w io.Writer) (err error) {
	zw := zip.NewWriter(w)
	defer zw.Close()

	// sharedStrings
	err = array2XlsxWriteSharedStrings(zw, data)
	if err != nil {
		return
	}
	// sheel1
	err = array2XlsxWriteSheet1(zw, data)
	if err != nil {
		return
	}

	for filename, content := range fixedFileContent {
		thisW, err := zw.Create(filename)
		if err != nil {
			return err
		}
		_, err = thisW.Write(content)
		if err != nil {
			return err
		}
	}

	return
}
Пример #28
0
// packToWriter packs given path object to io.Writer.
func packToWriter(srcPath string, w io.Writer, fn func(fullName string, fi os.FileInfo) error, includeDir bool) error {
	zw := zip.NewWriter(w)
	defer zw.Close()

	f, err := os.Open(srcPath)
	if err != nil {
		return err
	}
	defer f.Close()

	fi, err := f.Stat()
	if err != nil {
		return err
	}

	basePath := path.Base(srcPath)
	if fi.IsDir() {
		if includeDir {
			if err = packFile(srcPath, basePath, zw, fi); err != nil {
				return err
			}
		} else {
			basePath = ""
		}
		return packDir(srcPath, basePath, zw, fn)
	}
	return packFile(srcPath, basePath, zw, fi)
}
Пример #29
0
func dl_balance(w http.ResponseWriter, r *http.Request) {
	if !ipchecker(r) {
		return
	}

	wallet.UpdateBalanceFolder()
	buf := new(bytes.Buffer)
	zi := zip.NewWriter(buf)
	filepath.Walk("balance/", func(path string, fi os.FileInfo, err error) error {
		if !fi.IsDir() {
			f, _ := zi.Create(path)
			if f != nil {
				da, _ := ioutil.ReadFile(path)
				f.Write(da)
			}
		}
		return nil
	})
	if zi.Close() == nil {
		w.Header()["Content-Type"] = []string{"application/zip"}
		w.Write(buf.Bytes())
	} else {
		w.Write([]byte("Error"))
	}
}
Пример #30
0
// setups state each of the tests uses
func setup() error {
	// create zipfs
	b := new(bytes.Buffer)
	zw := zip.NewWriter(b)
	for file, contents := range files {
		w, err := zw.Create(file)
		if err != nil {
			return err
		}
		_, err = io.WriteString(w, contents)
		if err != nil {
			return err
		}
	}
	zw.Close()
	zr, err := zip.NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))
	if err != nil {
		return err
	}
	rc := &zip.ReadCloser{
		Reader: *zr,
	}
	fs = New(rc, "foo")

	// pull out different stat functions
	statFuncs = []statFunc{
		{"Stat", fs.Stat},
		{"Lstat", fs.Lstat},
	}

	return nil
}