Example #1
0
// Deal with file
func zipFile(srcFile string, recPath string, zw *zip.Writer, fi os.FileInfo) {
	if fi.IsDir() {
		// Create zip header
		fh := new(zip.FileHeader)
		fh.Name = recPath + "/"
		fh.UncompressedSize = 0

		_, err := zw.CreateHeader(fh)
		handleError(err)
	} else {
		// Create zip header
		fh := new(zip.FileHeader)
		fh.Name = recPath
		fh.UncompressedSize = uint32(fi.Size())
		fw, err := zw.CreateHeader(fh)
		handleError(err)

		// Read file data
		buf := make([]byte, fi.Size())
		f, err := os.Open(srcFile)
		handleError(err)
		_, err = f.Read(buf)
		handleError(err)

		// Write file data to zip
		_, err = fw.Write(buf)
		handleError(err)
	}
}
Example #2
0
func array2XlsxWriteSharedStrings(zw *zip.Writer, data [][]string) (err error) {
	siList := []xlsxSharedStringSi{{""}}
	for _, row := range data {
		for _, v1 := range row {
			if v1 == "" { //ignore blank cell can save space
				continue
			}
			siList = append(siList, xlsxSharedStringSi{v1})
		}
	}
	sst := xlsxSharedStringSst{
		Xmlns:  xmlNs,
		Count:  len(siList),
		SiList: siList,
	}
	thisW, err := zw.Create(sharedStringsFileName)
	_, err = thisW.Write([]byte(xml.Header))
	if err != nil {
		return
	}
	encoder := xml.NewEncoder(thisW)
	err = encoder.Encode(sst)
	if err != nil {
		return
	}
	return
}
Example #3
0
func AddContainer(zippy *zip.Writer) {
	container, err := zippy.Create(path.Join("META-INF", "container.xml"))
	if err != nil {
		log.Fatal(err)
	}
	WriteToFile(container, "<?xml version=\"1.0\"?><container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\"><rootfiles><rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/></rootfiles></container>")
}
Example #4
0
func AddMimetype(zippy *zip.Writer) {
	mimetype, err := zippy.Create("mimetype")
	if err != nil {
		log.Fatal(err)
	}
	WriteToFile(mimetype, "application/epub+zip")
}
Example #5
0
File: zip.go Project: jsli/gtbox
func writeFile2Zip(zw *zip.Writer, path string, name string) error {
	file, err := os.Open(path + name)
	if err != nil {
		return err
	}
	defer file.Close()

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

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

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

	_, err = io.Copy(writer, file)
	return err
}
Example #6
0
func addKeysToArchive(zipWriter *zip.Writer, newKeyStore *trustmanager.KeyFileStore) error {
	for _, relKeyPath := range newKeyStore.ListFiles() {
		fullKeyPath := filepath.Join(newKeyStore.BaseDir(), relKeyPath)

		fi, err := os.Lstat(fullKeyPath)
		if err != nil {
			return err
		}

		infoHeader, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}

		infoHeader.Name = relKeyPath

		zipFileEntryWriter, err := zipWriter.CreateHeader(infoHeader)
		if err != nil {
			return err
		}

		fileContents, err := ioutil.ReadFile(fullKeyPath)
		if err != nil {
			return err
		}

		if _, err = zipFileEntryWriter.Write(fileContents); err != nil {
			return err
		}
	}

	return nil
}
Example #7
0
func addKeysToArchive(zipWriter *zip.Writer, newKeyStore *trustmanager.KeyFileStore, subDir string) error {
	// List all files but no symlinks
	for _, relKeyPath := range newKeyStore.ListFiles(false) {
		fullKeyPath := filepath.Join(newKeyStore.BaseDir(), relKeyPath)

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

		infoHeader, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}

		infoHeader.Name = filepath.Join(subDir, relKeyPath)
		zipFileEntryWriter, err := zipWriter.CreateHeader(infoHeader)
		if err != nil {
			return err
		}

		fileContents, err := ioutil.ReadFile(fullKeyPath)
		if err != nil {
			return err
		}
		if _, err = zipFileEntryWriter.Write(fileContents); err != nil {
			return err
		}
	}

	return nil
}
Example #8
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
}
Example #9
0
func foliazipdir(q *Context, zf *zip.Writer, fdir, subdir string) (ok bool) {
	dir := filepath.Join(fdir, subdir)
	files, err := ioutil.ReadDir(dir)
	if foliaErr(q, err) {
		return false
	}
	for _, file := range files {
		fname := path.Join(subdir, file.Name()) // in zip alleen forward slashes toegestaan
		if file.IsDir() {
			if !foliazipdir(q, zf, fdir, fname) {
				return false
			}
		} else {
			data, err := ioutil.ReadFile(filepath.Join(fdir, fname))
			if foliaErr(q, err) {
				return false
			}
			fh, err := zip.FileInfoHeader(file)
			if foliaErr(q, err) {
				return false
			}
			fh.Name = fname
			f, err := zf.CreateHeader(fh)
			if foliaErr(q, err) {
				return false
			}
			_, err = f.Write(data)
			if foliaErr(q, err) {
				return false
			}
		}
	}
	return true
}
Example #10
0
func addFileToZip(w *zip.Writer, path string, newname string) error {
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	info, err := f.Stat()
	if err != nil {
		return err
	}
	header, err := zip.FileInfoHeader(info)
	if err != nil {
		return err
	}
	if newname != "" {
		header.Name = newname
	}
	wr, err := w.CreateHeader(header)
	if err != nil {
		return err
	}
	fileBytes, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}
	_, err = wr.Write(fileBytes)
	if err != nil {
		return err
	}
	return nil
}
Example #11
0
File: util.go Project: jquag/kempt
func addFileToZip(zipWriter *zip.Writer, fi os.FileInfo, srcPath string) error {
	fh, err := zip.FileInfoHeader(fi)
	if err != nil {
		return err
	}
	fh.Method = zip.Deflate
	destFile, err := zipWriter.CreateHeader(fh)
	if err != nil {
		return err
	}

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

	ba, err := ioutil.ReadAll(srcFile)
	if err != nil {
		return err
	}

	//_, err = io.Copy(destFile, srcFile)
	_, err = destFile.Write(ba)
	return err
}
Example #12
0
func addFileToZIP(zw *zip.Writer, item ArchiveItem) (err error) {
	binfo, err := os.Stat(item.FileSystemPath)
	if err != nil {
		return
	}
	header, err := zip.FileInfoHeader(binfo)
	if err != nil {
		return
	}
	header.Method = zip.Deflate
	//always use forward slashes even on Windows
	header.Name = strings.Replace(item.ArchivePath, "\\", "/", -1)
	w, err := zw.CreateHeader(header)
	if err != nil {
		zw.Close()
		return
	}
	bf, err := os.Open(item.FileSystemPath)
	if err != nil {
		return
	}
	defer bf.Close()
	_, err = io.Copy(w, bf)
	return
}
Example #13
0
func addKeysToArchive(zipWriter *zip.Writer, newKeyStore *trustmanager.KeyFileStore, tempBaseDir string) error {
	// List all files but no symlinks
	for _, fullKeyPath := range newKeyStore.ListFiles(false) {
		relKeyPath := strings.TrimPrefix(fullKeyPath, tempBaseDir)
		relKeyPath = strings.TrimPrefix(relKeyPath, string(filepath.Separator))

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

		infoHeader, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}

		infoHeader.Name = relKeyPath
		zipFileEntryWriter, err := zipWriter.CreateHeader(infoHeader)
		if err != nil {
			return err
		}

		fileContents, err := ioutil.ReadFile(fullKeyPath)
		if err != nil {
			return err
		}
		if _, err = zipFileEntryWriter.Write(fileContents); err != nil {
			return err
		}
	}

	return nil
}
Example #14
0
func addZipEntry(zw *zip.Writer, f filetesting.Entry) {
	h := &zip.FileHeader{
		Name: f.GetPath(),
		// Don't bother compressing - the contents are so small that
		// it will just slow things down for no particular benefit.
		Method: zip.Store,
	}
	contents := ""
	switch f := f.(type) {
	case filetesting.Dir:
		h.SetMode(os.ModeDir | 0755)
	case filetesting.File:
		h.SetMode(f.Perm)
		contents = f.Data
	case filetesting.Symlink:
		h.SetMode(os.ModeSymlink | 0777)
		contents = f.Link
	}
	w, err := zw.CreateHeader(h)
	if err != nil {
		panic(err)
	}
	if contents != "" {
		if _, err := w.Write([]byte(contents)); err != nil {
			panic(err)
		}
	}
}
Example #15
0
// 逐个文件,写入zip包
// zipper *zip.Writer:要写入的zip包的zip.Writer
// includeFileName string:本次写入的文件的全路径+名字
func writeFileToZip(zipper *zip.Writer, includeFileName string) error {
	//打开待写入文件
	includeFile, err := os.Open(includeFileName)
	if err != nil {
		return err
	}
	defer includeFile.Close()

	//获取文件描述
	includeFileInfo, err := includeFile.Stat()
	if err != nil {
		return err
	}

	//zip.FileInfoHeader
	zipFileHeader, err := zip.FileInfoHeader(includeFileInfo)
	if err != nil {
		return err
	}

	//修改文件描述的Header,截断路径,只保留文件名
	//否则,解压的时候,可能按压缩进来时候的文件路径来解压,解压回到原来的位置,而不是当前目录
	zipFileHeader.Name = path.Base(includeFileName)

	//用zip.FileInfoHeader,创建zip包内的一个项,并获得io.Writer,准备写入文件
	zipFileWriter, err := zipper.CreateHeader(zipFileHeader)
	if err != nil {
		return err
	}

	//写入本次的文件
	_, err = io.Copy(zipFileWriter, includeFile)
	return err
}
Example #16
0
// addIndexToZip generates an index.html file for the given Overlay and adds
// it to the provided zip file.
func addIndexToZip(c appengine.Context, z *zip.Writer, oKey *datastore.Key, o *Overlay) error {
	w, err := z.Create(fmt.Sprintf("%s/index.html", oKey.Encode()))
	if err != nil {
		return err
	}
	return zipTemplate.Execute(w, o)
}
Example #17
0
func addZip(filename, buildDir string, zipWriter *zip.Writer) error {
	zipfile, err := os.Open(filename)
	if err != nil {
		return err
	}
	defer zipfile.Close()

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

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

	r := regexp.MustCompile(buildDir + "/")
	header.Name = r.ReplaceAllString(filename, "")
	writer, err := zipWriter.CreateHeader(header)
	if err != nil {
		return err
	}

	_, err = io.Copy(writer, zipfile)
	return nil
}
Example #18
0
func addHandlerToZip(w *zip.Writer, bf baofile.Baofile) error {
	path := "lambda/handler_example.js"
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	info, err := f.Stat()
	if err != nil {
		return err
	}
	header, err := zip.FileInfoHeader(info)
	if err != nil {
		return err
	}
	wr, err := w.CreateHeader(header)
	if err != nil {
		return err
	}
	fileBytes, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}

	handlerTemplate := string(fileBytes)
	t := template.Must(template.New("handler").Parse(handlerTemplate))
	err = t.Execute(wr, bf)
	if err != nil {
		return err
	}
	return nil
}
Example #19
0
// Deal with files
func zipFile(srcFile string, recPath string, tw *zip.Writer, fi os.FileInfo) {
	if fi.IsDir() {
		//    	fmt.Println("??")
		// Create tar header
		/*
		   fh, err := zip.FileInfoHeader(fi)
		   if err != nil {
		       panic(err)
		   }
		   fh.Name = recPath // + "/"
		   err = tw.WriteHeader(hdr)
		   tw.Create(recPath)
		*/
	} else {
		// File reader
		fr, err := os.Open(srcFile)
		if err != nil {
			panic(err)
		}
		defer fr.Close()

		// Write hander
		w, err2 := tw.Create(recPath)
		if err2 != nil {
			panic(err)
		}
		// Write file data
		_, err = io.Copy(w, fr)
		if err != nil {
			panic(err)
		}
	}
}
Example #20
0
// Writes this tree to the given zip file, returning an error on failure.
func (t Stream) ToZip(w *zip.Writer) error {
	for blob := range t {

		hdr, err := zip.FileInfoHeader(blob)
		if err != nil {
			return err
		}

		writer, err := w.CreateHeader(hdr)
		if err != nil {
			return err
		}

		if blob.Contents() != nil {
			_, err := io.Copy(writer, blob.Contents())
			if err != nil {
				return err
			}
		}

		if blob.Error() != nil {
			return blob.Error()
		}
	}

	w.Close()

	return nil
}
Example #21
0
func writeFileToZip(zipper *zip.Writer, filename string) error {
	file, err := os.Open(filename)

	if err != nil {
		return err
	}

	defer file.Close()

	info, err := file.Stat()

	if err != nil {
		return err
	}

	header, err := zip.FileInfoHeader(info)

	if err != nil {
		return err
	}

	header.Name = helper.SanitizedName(filename)

	writer, err := zipper.CreateHeader(header)

	if err != nil {
		return err
	}

	_, err = io.Copy(writer, file)

	return err
}
Example #22
0
// Copy any files from the includes directory
func copyIncludes(zw *zip.Writer, includes string) error {
	includes = path.Clean(includes)
	if !strings.HasSuffix(includes, "/") {
		includes += "/"
	}
	return filepath.Walk(includes, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}
		path = filepath.ToSlash(path)
		if w, err := zw.Create(strings.TrimPrefix(path, includes)); err != nil {
			return err
		} else {
			b, err := ioutil.ReadFile(path)
			if err != nil {
				return err
			} else {
				_, err := w.Write(b)
				if err != nil {
					return err
				}
			}
		}
		return nil
	})
}
Example #23
0
func packFile(srcFile string, recPath string, zw *zip.Writer, fi os.FileInfo) (err error) {
	if fi.IsDir() {
		// Create zip header
		fh := new(zip.FileHeader)
		fh.Name = recPath + "/"
		fh.UncompressedSize = 0

		_, err = zw.CreateHeader(fh)
	} else {
		// Create zip header
		fh := new(zip.FileHeader)
		fh.Name = recPath
		fh.UncompressedSize = uint32(fi.Size())
		fh.SetMode(fi.Mode())
		var fw io.Writer
		fw, err = zw.CreateHeader(fh)
		if err != nil {
			return err
		}

		var f *os.File
		f, err = os.Open(srcFile)
		if err != nil {
			return err
		}
		_, err = io.Copy(fw, f)
	}
	return err
}
Example #24
0
File: file.go Project: krabken/xlsx
// 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()
}
Example #25
0
func addtozip(z *zip.Writer, name string, f *os.File) {

	if fi, _ := f.Stat(); fi.IsDir() {
		log.Println("Adding folder", name)
		names, _ := f.Readdirnames(-1)
		for _, subname := range names {
			file, err := os.Open(filepath.Join(f.Name(), subname))
			if err != nil {
				log.Println(err)
				continue
			}
			addtozip(z, filepath.Join(name, subname), file)
			file.Close()
		}
	} else {
		log.Println("Adding file", name)
		fw, err := z.Create(name)
		if err != nil {
			panic(err)
		}
		_, err = io.Copy(fw, f)
		if err != nil {
			panic(err)
		}
	}
}
Example #26
0
func AddTitle(book_title string, book_author string, zippy *zip.Writer) {
	title, err := zippy.Create(path.Join("OEBPS", "title.xhtml"))
	if err != nil {
		log.Fatal(err)
	}

	WriteToFile(title, fmt.Sprintf("<html>\n\t<head>\n\t\t<title>%s</title>\n\t</head>\n\t<body>\n\t\t<center><h1>%s</h1>\n\t\t<h2>by %s</h2></center>\n\t</body>\n</html>", book_title, book_title, book_author))
}
Example #27
0
func copyFile(outzip *zip.Writer, name string, in *zip.File) {
	w, err := outzip.Create(name)
	must(err)
	r, err := in.Open()
	must(err)
	defer r.Close()
	_, err = io.Copy(w, r)
	must(err)
}
Example #28
0
func addZipFileMust(w *zip.Writer, path string) {
	d, err := ioutil.ReadFile(path)
	fataliferr(err)
	f, err := w.Create(filepath.Base(path))
	fataliferr(err)
	_, err = f.Write(d)
	fataliferr(err)
	// no need to close f. It's implicitly closed by the next Create() or Close() call
}
Example #29
0
func walk(base, startPath string, zipWriter *zip.Writer) error {
	return filepath.Walk(startPath, func(path string, stat os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if stat.IsDir() {
			return nil
		}

		relativePath, err := filepath.Rel(startPath, path)
		if err != nil {
			return err
		}

		filePath := path
		if stat.Mode()&os.ModeSymlink != 0 {
			filePath, err = filepath.EvalSymlinks(path)
			if err != nil {
				return err
			}
			stat, err = os.Stat(filePath)
			if err != nil {
				return err
			}
			if stat.IsDir() {
				if err != nil {
					return err
				}
				return walk(Path.Join(base, ".", relativePath), filePath, zipWriter)
			}
		}

		name := Path.Join(base, filepath.ToSlash(relativePath))

		dbg("+", filePath, "=>", name)

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

		writer, err := zipWriter.Create(name)
		if err != nil {
			return err
		}

		_, err = io.Copy(writer, file)
		if err != nil {
			return err
		}

		return nil
	})
}
Example #30
0
func (w *Writer) writeFile(z *zip.Writer, s string, b []byte) error {
	f, err := z.Create(s)
	if err != nil {
		return err
	}

	_, err = f.Write(b)

	return err
}