func (zipper ApplicationZipper) extractFile(f *zip.File, destDir string) error {
	if f.FileInfo().IsDir() {
		os.MkdirAll(filepath.Join(destDir, f.Name), os.ModeDir|os.ModePerm)
		return nil
	}

	var rc io.ReadCloser
	rc, err := f.Open()
	if err != nil {
		return err
	}

	defer rc.Close()

	destFilePath := filepath.Join(destDir, f.Name)

	err = fileutils.CopyReaderToPath(rc, destFilePath)
	if err != nil {
		return err
	}

	err = os.Chmod(destFilePath, f.FileInfo().Mode())
	if err != nil {
		return err
	}

	return nil
}
Beispiel #2
0
func (repo CloudControllerApplicationBitsRepository) extractZip(appDir, destDir string) (err error) {
	r, err := zip.OpenReader(appDir)
	if err != nil {
		return
	}
	defer r.Close()

	for _, f := range r.File {
		func() {
			// Don't try to extract directories
			if f.FileInfo().IsDir() {
				return
			}

			var rc io.ReadCloser
			rc, err = f.Open()
			if err != nil {
				return
			}

			// functional scope from above is important
			// otherwise this only closes the last file handle
			defer rc.Close()

			destFilePath := filepath.Join(destDir, f.Name)

			err = fileutils.CopyReaderToPath(rc, destFilePath)
			if err != nil {
				return
			}

			err = os.Chmod(destFilePath, f.FileInfo().Mode())
			if err != nil {
				return
			}
		}()
	}

	return
}
Beispiel #3
0
		})

		It("copies the file contents", func() {
			fileBytes, err := ioutil.ReadFile(destPath)
			Expect(err).NotTo(HaveOccurred())

			Expect(fileBytes).To(Equal(fixtureBytes))
		})
	})

	Describe("CopyReaderToPath", func() {
		var destPath = fileutils.TempPath("copy_test")

		BeforeEach(func() {
			fixtureReader, err := os.Open(fixturePath)
			Expect(err).NotTo(HaveOccurred())
			defer fixtureReader.Close()

			err = fileutils.CopyReaderToPath(fixtureReader, destPath)
			Expect(err).NotTo(HaveOccurred())
		})

		It("copies the file contents", func() {
			fileBytes, err := ioutil.ReadFile(destPath)
			Expect(err).NotTo(HaveOccurred())

			Expect(fileBytes).To(Equal(fixtureBytes))
		})
	})
})