Ejemplo n.º 1
0
// StreamFile streams a file or directory entry into StreamArchive.
func (s *StreamArchive) StreamFile(relPath string, fi os.FileInfo, data []byte) error {
	if fi.IsDir() {
		fh, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}
		fh.Name = relPath + "/"
		if _, err = s.Writer.CreateHeader(fh); err != nil {
			return err
		}
	} else {
		fh, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}
		fh.Name = filepath.Join(relPath, fi.Name())
		fh.Method = zip.Deflate
		fw, err := s.Writer.CreateHeader(fh)
		if err != nil {
			return err
		} else if _, err = fw.Write(data); err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 2
0
func addFileToZIP(zw *zip.Writer, item ArchiveItem) error {
	//fmt.Printf("Adding %s\n", item.FileSystemPath)
	binfo, err := os.Stat(item.FileSystemPath)
	if err != nil {
		return err
	}
	if binfo.IsDir() {
		header, err := zip.FileInfoHeader(binfo)
		if err != nil {
			return err
		}
		header.Method = zip.Deflate
		header.Name = item.ArchivePath
		_, err = zw.CreateHeader(header)
		if err != nil {
			return err
		}
		file, err := os.Open(item.FileSystemPath)
		if err != nil {
			return err
		}
		fis, err := file.Readdir(0)
		for _, fi := range fis {
			err = addFileToZIP(zw, ArchiveItem{filepath.Join(item.FileSystemPath, fi.Name()), filepath.Join(item.ArchivePath, fi.Name()), nil})
			if err != nil {
				return err
			}
		}
	} else {
		header, err := zip.FileInfoHeader(binfo)
		if err != nil {
			return err
		}
		header.Method = zip.Deflate
		header.Name = item.ArchivePath
		w, err := zw.CreateHeader(header)
		if err != nil {
			return err
		}
		bf, err := os.Open(item.FileSystemPath)
		if err != nil {
			return err
		}
		defer bf.Close()
		_, err = io.Copy(w, bf)
		if err != nil {
			return err
		}
	}
	return err
}
Ejemplo n.º 3
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
}
Ejemplo n.º 4
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
}
Ejemplo n.º 5
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
}
Ejemplo n.º 6
0
func (wft *zipWalk) compress(name, fpath string, fi os.FileInfo) (bool, error) {
	isSym := fi.Mode()&os.ModeSymlink > 0
	if isSym {
		// golang1.1 doesn't support embed symlink
		// what i miss something?
		return false, nil
	}

	hdr, err := zip.FileInfoHeader(fi)
	if err != nil {
		return false, err
	}
	hdr.Name = name

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

	if isSym == false {
		fr, err := os.Open(fpath)
		if err != nil {
			return false, err
		}
		defer fr.Close()
		_, err = io.Copy(w, fr)
		if err != nil {
			return false, err
		}
	}

	return true, nil
}
Ejemplo n.º 7
0
func (z *zipRenderer) Walk(path string, finfo os.FileInfo, err error) error {
	if err != nil {
		return err
	}

	if finfo.IsDir() {
		return nil
	}

	fh, err := zip.FileInfoHeader(finfo)
	if err != nil {
		return err
	}

	fh.Name = "download/" + path
	w, err := z.w.CreateHeader(fh)
	if err != nil {
		return err
	}

	f, err := os.Open(filepath.Join(z.root, path))
	if err != nil {
		return err
	}
	defer f.Close()

	_, err = io.Copy(w, f)
	return err
}
Ejemplo n.º 8
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
}
Ejemplo n.º 9
0
func createZipEntry(archive *zip.Writer, fileName string) error {
	fi, err := os.Lstat(fileName)
	if err != nil {
		logrus.Warningln("File ignored:", err)
		return nil
	}

	fh, err := zip.FileInfoHeader(fi)
	if err != nil {
		return err
	}
	fh.Name = fileName
	fh.Extra = createZipExtra(fi)

	switch fi.Mode() & os.ModeType {
	case os.ModeDir:
		return createZipDirectoryEntry(archive, fh)

	case os.ModeSymlink:
		return createZipSymlinkEntry(archive, fh)

	case os.ModeNamedPipe, os.ModeSocket, os.ModeDevice:
		// Ignore the files that of these types
		logrus.Warningln("File ignored:", fileName)
		return nil

	default:
		return createZipFileEntry(archive, fh)
	}
}
Ejemplo n.º 10
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
}
Ejemplo n.º 11
0
func (z *ZipFile) AddEntry(path, name string) error {
	fi, err := os.Stat(name)
	if err != nil {
		return err
	}

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

	fh.Name = path
	fh.Method = zip.Deflate // data compression algorithm

	entry, err := z.writer.CreateHeader(fh)
	if err != nil {
		return err
	}

	if fi.IsDir() {
		return nil
	}

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

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

	return err
}
Ejemplo n.º 12
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
}
Ejemplo n.º 13
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
}
Ejemplo n.º 14
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
}
Ejemplo n.º 15
0
Archivo: util.go Proyecto: 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
}
Ejemplo n.º 16
0
// Add file to zip.
func (z *Zipper) addFile(filePath string) error {
	// Get file information.
	fileInfo, err := os.Stat(filePath)
	if err != nil {
		return err
	}
	// Get file content.
	content, err := ioutil.ReadFile(filePath)
	if err != nil {
		return err
	}
	// Set zip header.
	header, _ := zip.FileInfoHeader(fileInfo)
	path := filepath.Join(z.basePath, filePath)
	header.Name = path
	zipFile, err := z.writer.CreateHeader(header)
	if err != nil {
		return err
	}
	// Write content.
	_, err = zipFile.Write(content)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 17
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
}
Ejemplo n.º 18
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
}
Ejemplo n.º 19
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
}
Ejemplo n.º 20
0
func TestProcessZipExtra(t *testing.T) {
	testFile, err := ioutil.TempFile("", "test")
	assert.NoError(t, err)
	defer testFile.Close()
	defer os.Remove(testFile.Name())

	fi, _ := testFile.Stat()
	assert.NotNil(t, fi)

	zipFile, err := zip.FileInfoHeader(fi)
	assert.NoError(t, err)
	zipFile.Extra = createZipExtra(fi)

	err = ioutil.WriteFile(fi.Name(), []byte{}, 0666)
	defer os.Remove(fi.Name())
	assert.NoError(t, err)

	err = processZipExtra(zipFile)
	assert.NoError(t, err)

	fi2, _ := testFile.Stat()
	assert.NotNil(t, fi2)
	assert.Equal(t, fi.Mode(), fi2.Mode())
	assert.Equal(t, fi.ModTime(), fi2.ModTime())
}
Ejemplo n.º 21
0
Archivo: zip.go Proyecto: 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
}
Ejemplo n.º 22
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
}
Ejemplo n.º 23
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
}
Ejemplo n.º 24
0
Archivo: base.go Proyecto: AlekSi/nut
// Pack files into nut file with given fileName.
func PackNut(fileName string, files []string, verbose bool) {
	// write nut to temporary file first
	nutFile, err := ioutil.TempFile("", "nut-")
	FatalIfErr(err)
	defer func() {
		if nutFile != nil {
			FatalIfErr(os.Remove(nutFile.Name()))
		}
	}()

	nutWriter := zip.NewWriter(nutFile)
	defer func() {
		if nutWriter != nil {
			FatalIfErr(nutWriter.Close())
		}
	}()

	// add files to nut with all meta information
	for _, file := range files {
		if verbose {
			log.Printf("Packing %s ...", file)
		}

		fi, err := os.Stat(file)
		FatalIfErr(err)

		fh, err := zip.FileInfoHeader(fi)
		FatalIfErr(err)
		fh.Name = file

		f, err := nutWriter.CreateHeader(fh)
		FatalIfErr(err)

		b, err := ioutil.ReadFile(file)
		FatalIfErr(err)

		_, err = f.Write(b)
		FatalIfErr(err)
	}

	err = nutWriter.Close()
	nutWriter = nil
	FatalIfErr(err)

	FatalIfErr(nutFile.Close())

	// move file to specified location and fix permissions
	if verbose {
		log.Printf("Creating %s ...", fileName)
	}
	_, err = os.Stat(fileName)
	if err == nil {
		// required on Windows
		FatalIfErr(os.Remove(fileName))
	}
	FatalIfErr(os.Rename(nutFile.Name(), fileName))
	nutFile = nil
	FatalIfErr(os.Chmod(fileName, NutFilePerm))
}
Ejemplo n.º 25
0
func writeZipFile(dir string, targetFile *os.File) error {
	isEmpty, err := fileutils.IsDirEmpty(dir)
	if err != nil {
		return err
	}

	if isEmpty {
		return errors.NewEmptyDirError(dir)
	}

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

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

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

		if runtime.GOOS == "windows" {
			header.SetMode(header.Mode() | 0700)
		}

		header.Name = filepath.ToSlash(fileName)
		header.Method = zip.Deflate

		if fileInfo.IsDir() {
			header.Name += "/"
		}

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

		if fileInfo.IsDir() {
			return nil
		}

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

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

		return nil
	})
}
Ejemplo n.º 26
0
// makeZip creates a zip archive. More or less a straight copy of
// makerelease.go from the go source tree.
func makeZip(targ, workdir string) error {
	f, err := os.Create(targ)
	if err != nil {
		return err
	}
	zw := zip.NewWriter(f)

	err = filepath.Walk(workdir, func(path string, fi os.FileInfo, err error) error {
		if !strings.HasPrefix(path, workdir) {
			log.Panicf("walked filename %q doesn't begin with workdir %q", path, workdir)
		}
		name := path[len(workdir):]

		// Convert to Unix-style named paths, as that's the
		// type of zip file that archive/zip creates.
		name = strings.Replace(name, "\\", "/", -1)
		// Chop of any leading / from filename, leftover from removing workdir.
		if strings.HasPrefix(name, "/") {
			name = name[1:]
		}

		if name == "" {
			return nil
		}

		fh, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}
		fh.Name = name
		fh.Method = zip.Deflate
		if fi.IsDir() {
			fh.Name += "/"        // append trailing slash
			fh.Method = zip.Store // no need to deflate 0 byte files
		}
		w, err := zw.CreateHeader(fh)
		if err != nil {
			return err
		}
		if fi.IsDir() {
			return nil
		}
		r, err := os.Open(path)
		if err != nil {
			return err
		}
		defer r.Close()
		_, err = io.Copy(w, r)
		return err
	})
	if err != nil {
		return err
	}
	if err := zw.Close(); err != nil {
		return err
	}
	return f.Close()
}
Ejemplo n.º 27
0
func normalizeBuildpackArchive(inputFile *os.File, outputFile *os.File) error {
	stats, err := inputFile.Stat()
	if err != nil {
		return err
	}

	reader, err := zip.NewReader(inputFile, stats.Size())
	if err != nil {
		return err
	}

	contents := reader.File

	parentPath, hasBuildpack := findBuildpackPath(contents)

	if !hasBuildpack {
		return errors.New(T("Zip archive does not contain a buildpack"))
	}

	writer := zip.NewWriter(outputFile)

	for _, file := range contents {
		name := file.Name
		if strings.HasPrefix(name, parentPath) {
			relativeFilename := strings.TrimPrefix(name, parentPath+"/")
			if relativeFilename == "" {
				continue
			}

			fileInfo := file.FileInfo()
			header, err := zip.FileInfoHeader(fileInfo)
			if err != nil {
				return err
			}
			header.Name = relativeFilename

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

			r, err := file.Open()
			if err != nil {
				return err
			}

			io.Copy(w, r)
			err = r.Close()
			if err != nil {
				return err
			}
		}
	}

	writer.Close()
	outputFile.Seek(0, 0)
	return nil
}
Ejemplo n.º 28
0
// packFile packs a file or directory to zip.Writer.
func packFile(srcFile string, recPath string, zw *zip.Writer, fi os.FileInfo) error {
	if fi.IsDir() {
		fh, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}
		fh.Name = recPath + "/"
		if _, err = zw.CreateHeader(fh); err != nil {
			return err
		}
	} else {
		fh, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}
		fh.Name = recPath
		fh.Method = zip.Deflate

		fw, err := zw.CreateHeader(fh)
		if err != nil {
			return err
		}

		if fi.Mode()&os.ModeSymlink != 0 {
			target, err := os.Readlink(srcFile)
			if err != nil {
				return err
			}
			if _, err = fw.Write([]byte(target)); err != nil {
				return err
			}
		} else {
			f, err := os.Open(srcFile)
			if err != nil {
				return err
			}
			defer f.Close()

			if _, err = io.Copy(fw, f); err != nil {
				return err
			}
		}
	}
	return nil
}
Ejemplo n.º 29
0
func zipFolderFiles(source, target string) (err error) {
	zipfile, err := os.Create(target)
	if err != nil {
		cliutils.CheckError(err)
		return
	}
	defer func() {
		if cerr := zipfile.Close(); cerr != nil && err == nil {
			err = cerr
		}
	}()

	archive := zip.NewWriter(zipfile)
	defer func() {
		if cerr := archive.Close(); cerr != nil && err == nil {
			err = cerr
		}
	}()

	filepath.Walk(source, func(path string, info os.FileInfo, err error) (currentErr error) {
		if info.IsDir() {
			return
		}

		if err != nil {
			currentErr = err
			return
		}

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

		header.Method = zip.Deflate
		writer, currentErr := archive.CreateHeader(header)
		if currentErr != nil {
			cliutils.CheckError(currentErr)
			return
		}

		file, currentErr := os.Open(path)
		if currentErr != nil {
			cliutils.CheckError(currentErr)
			return
		}
		defer func() {
			if cerr := file.Close(); cerr != nil && currentErr == nil {
				currentErr = cerr
			}
		}()
		_, currentErr = io.Copy(writer, file)
		return
	})
	return
}
Ejemplo n.º 30
0
func normalizeBuildpackArchive(inputFile *os.File, outputFile *os.File) (err error) {
	stats, err := inputFile.Stat()
	if err != nil {
		return
	}

	reader, err := zip.NewReader(inputFile, stats.Size())
	if err != nil {
		return
	}

	contents := reader.File

	parentPath, hasBuildpack := findBuildpackPath(contents)

	if !hasBuildpack {
		return errors.New("Zip archive does not contain a buildpack")
	}

	writer := zip.NewWriter(outputFile)

	for _, file := range contents {
		name := file.Name
		if parentPath == "." || strings.HasPrefix(name, parentPath) {
			var (
				r      io.ReadCloser
				w      io.Writer
				header *zip.FileHeader
			)

			fileInfo := file.FileInfo()
			header, err = zip.FileInfoHeader(fileInfo)
			header.Name = strings.Replace(name, parentPath+"/", "", 1)

			r, err = file.Open()
			if err != nil {
				return
			}

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

			io.Copy(w, r)
			err = r.Close()
			if err != nil {
				return
			}
		}
	}

	writer.Close()
	outputFile.Seek(0, 0)
	return
}