Example #1
0
func parseStyles(f *zip.File) {

	s, err := f.Open()
	if err != nil {
		panic(err)
	}

	x := XMapStyle{}
	content, err := ioutil.ReadAll(s)
	if err != nil {
		panic(err)
	}
	xml.Unmarshal(content, &x)

	for _, s := range x.Styles {

		mdmark := []string{""}
		for _, p := range s.Properties {
			if p.Decoration == "underline" {
				mdmark = append(mdmark, "u")
			}
			if p.Style == "italic" {
				mdmark = append(mdmark, "em")
			}
			if p.Weight == "bold" {
				mdmark = append(mdmark, "strong")
			}

			STYLES[s.Id] = append(STYLES[s.Id], mdmark...)
		}

	}
	defer s.Close()
}
Example #2
0
// readWorkbookRelationsFromZipFile is an internal helper function to
// extract a map of relationship ID strings to the name of the
// worksheet.xml file they refer to.  The resulting map can be used to
// reliably derefence the worksheets in the XLSX file.
func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (WorkBookRels, error) {
	var sheetXMLMap WorkBookRels
	var wbRelationships *xlsxWorkbookRels
	var rc io.ReadCloser
	var decoder *xml.Decoder
	var err error

	rc, err = workbookRels.Open()
	if err != nil {
		return nil, err
	}
	decoder = xml.NewDecoder(rc)
	wbRelationships = new(xlsxWorkbookRels)
	err = decoder.Decode(wbRelationships)
	if err != nil {
		return nil, err
	}
	sheetXMLMap = make(WorkBookRels)
	for _, rel := range wbRelationships.Relationships {
		if strings.HasSuffix(rel.Target, ".xml") && rel.Type == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" {
			_, filename := path.Split(rel.Target)
			sheetXMLMap[rel.Id] = strings.Replace(filename, ".xml", "", 1)
		}
	}
	return sheetXMLMap, nil
}
Example #3
0
// readWorkbookRelationsFromZipFile is an internal helper function to
// extract a map of relationship ID strings to the name of the
// worksheet.xml file they refer to.  The resulting map can be used to
// reliably derefence the worksheets in the XLSX file.
func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (map[string]string, error) {
	var sheetXMLMap map[string]string
	var wbRelationships *xlsxWorkbookRels
	var rc io.ReadCloser
	var decoder *xml.Decoder
	var err error

	rc, err = workbookRels.Open()
	if err != nil {
		return nil, err
	}
	decoder = xml.NewDecoder(rc)
	wbRelationships = new(xlsxWorkbookRels)
	err = decoder.Decode(wbRelationships)
	if err != nil {
		return nil, err
	}
	sheetXMLMap = make(map[string]string)
	for _, rel := range wbRelationships.Relationships {
		if strings.HasSuffix(rel.Target, ".xml") && strings.HasPrefix(rel.Target, "worksheets/") {
			sheetXMLMap[rel.Id] = strings.Replace(rel.Target[len("worksheets/"):], ".xml", "", 1)
		}
	}
	return sheetXMLMap, nil
}
Example #4
0
func parseIpaIcon(path string) (data []byte, err error) {
	iconPattern := regexp.MustCompile(`(?i)^Payload/[^/]*/icon\.png$`)
	r, err := zip.OpenReader(path)
	if err != nil {
		return
	}
	defer r.Close()

	var zfile *zip.File
	for _, file := range r.File {
		if iconPattern.MatchString(file.Name) {
			zfile = file
			break
		}
	}
	if zfile == nil {
		err = errors.New("icon.png file not found")
		return
	}
	plreader, err := zfile.Open()
	if err != nil {
		return
	}
	defer plreader.Close()
	return ioutil.ReadAll(plreader)
}
Example #5
0
// readSheetsFromZipFile is an internal helper function that loops
// over the Worksheets defined in the XSLXWorkbook and loads them into
// Sheet objects stored in the Sheets slice of a xlsx.File struct.
func readSheetsFromZipFile(f *zip.File, file *File) ([]*Sheet, error) {
	var workbook *XLSXWorkbook
	var error error
	var rc io.ReadCloser
	workbook = new(XLSXWorkbook)
	rc, error = f.Open()
	if error != nil {
		return nil, error
	}
	error = xml.Unmarshal(rc, workbook)
	if error != nil {
		return nil, error
	}
	sheets := make([]*Sheet, len(workbook.Sheets.Sheet))
	for i, rawsheet := range workbook.Sheets.Sheet {
		worksheet, error := getWorksheetFromSheet(rawsheet, file.worksheets) //
		if error != nil {
			return nil, error
		}
		sheet := new(Sheet)
		sheet.Rows = readRowsFromSheet(worksheet, file.referenceTable)
		sheets[i] = sheet
	}
	return sheets, nil
}
Example #6
0
func unzipFile(file *zip.File, to string) (exec string, err error) {
	frc, err := file.Open()
	if err != nil {
		err = fmt.Errorf("Can't open zip entry %s when reading: %s", file.Name, err)
		return
	}
	defer frc.Close()

	dest := filepath.Join(to, filepath.Base(file.Name))
	f, err := os.Create(dest)
	if err != nil {
		return
	}
	defer f.Close()

	copied, err := io.Copy(f, frc)
	if err != nil {
		return
	}

	if uint32(copied) != file.UncompressedSize {
		err = fmt.Errorf("Zip entry %s is corrupted", file.Name)
		return
	}

	exec = f.Name()

	return
}
Example #7
0
func extractFile(f *zip.File, dest, md5 string) error {
	rc, err := f.Open()
	if err != nil {
		return err
	}
	defer func() {
		if err := rc.Close(); err != nil {
			panic(err)
		}
	}()

	path := filepath.Join(dest, md5+".dex")
	fout, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
	if err != nil {
		return err
	}
	defer func() {
		if err := fout.Close(); err != nil {
			panic(err)
		}
	}()

	_, err = io.Copy(fout, rc)
	if err != nil {
		return err
	}

	return nil
}
Example #8
0
func (z *unzipper) unzip(f *zip.File) error {
	if f.FileInfo().IsDir() {
		return nil
	}

	fName := filepath.Join(z.dst, f.Name)
	dir, _ := filepath.Split(fName)

	if err := os.MkdirAll(dir, perm); err != nil && os.IsNotExist(err) {
		return err
	}

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

	w, err := os.Create(filepath.Join(z.dst, f.Name))
	if err != nil {
		return err
	}
	defer w.Close()

	if _, err := io.CopyBuffer(w, r, z.buffer); err != nil {
		w.Close()
		return err
	}

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

	return w.Close()
}
Example #9
0
func copyFile(dst string, f *zip.File) (err error) {
	path := filepath.Join(dst, f.Name)
	if err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm); err != nil {
		return fmt.Errorf("mkdir failure:%s", err)
	}

	rc, err := f.Open()
	if err != nil {
		return fmt.Errorf("open failure:%s", err)
	}

	defer rc.Close()

	if !f.FileInfo().IsDir() {
		copied, err := os.Create(path)
		if err != nil {
			return err
		}
		defer copied.Close()

		if _, err := io.Copy(copied, rc); err != nil {
			return fmt.Errorf("copy failure:%s", err)
		}
	}

	return nil
}
Example #10
0
func unarchiveFile(zipFile *zip_impl.File, outFilePath string, progress ProgressFunc) error {
	if zipFile.FileInfo().IsDir() {
		return nil
	}

	if progress != nil {
		progress(zipFile.Name)
	}

	zipFileReader, err := zipFile.Open()
	if err != nil {
		return err
	}
	defer func() {
		_ = zipFileReader.Close()
	}()

	filePath := filepath.Join(outFilePath, filepath.Join(strings.Split(zipFile.Name, "/")...))

	err = os.MkdirAll(filepath.Dir(filePath), os.FileMode(0755))
	if err != nil {
		return err
	}

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

	_, err = io.Copy(file, zipFileReader)
	return err
}
Example #11
0
// Writes the contents of a single file contained in webdriver.xpi
func (s *Firefox) writeExtensionFile(f *zip.File) (err error) {

	// seperated this into a function so I could use defer, etc.
	var file io.ReadCloser

	if file, err = f.Open(); err == nil {
		defer file.Close()

		targetFileSpec := filepath.Join(s.Extension.TargetPath, f.Name)
		if f.FileInfo().IsDir() {
			err = os.MkdirAll(targetFileSpec, s.DirPermissions)
		} else {

			var targetFile *os.File
			if targetFile, err = os.OpenFile(targetFileSpec,
				os.O_WRONLY|os.O_CREATE,
				s.FilePermissions); err == nil {

				defer targetFile.Close()
				_, err = io.Copy(targetFile, file)

			}

		}

	}

	return err
}
Example #12
0
// createTempFile creates a temporary file with the contents of the
// zip file. Used to implement io.Seeker interface.
func createTempFile(f *zip.File) (*os.File, error) {
	reader, err := f.Open()
	if err != nil {
		return nil, err
	}
	defer reader.Close()

	tempFile, err := ioutil.TempFile("", "zipfs")
	if err != nil {
		return nil, err
	}

	_, err = io.Copy(tempFile, reader)
	if err != nil {
		tempFile.Close()
		os.Remove(tempFile.Name())
		return nil, err
	}
	_, err = tempFile.Seek(0, os.SEEK_SET)
	if err != nil {
		tempFile.Close()
		os.Remove(tempFile.Name())
		return nil, err
	}

	return tempFile, nil
}
Example #13
0
func openFile(file *zip.File) (fs.File, error) {
	rc, err := file.Open()
	if err != nil {
		return nil, err
	}
	return &zipfile{file, rc}, nil
}
Example #14
0
func extractZipItem(f *zip.File, dest string) error {
	rc, err := f.Open()
	if err != nil {
		return err
	}
	defer rc.Close()

	path := filepath.Join(dest, f.Name)
	if f.FileInfo().IsDir() {
		os.MkdirAll(path, f.Mode())
	} else {
		f, err := os.OpenFile(
			path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
		if err != nil {
			return err
		}
		defer f.Close()

		_, err = io.Copy(f, rc)
		if err != nil {
			return err
		}
	}
	return nil
}
Example #15
0
func (p Pkg) copyBin(bin *zip.File, nw io.Reader, binName string, destDir string) error {
	r, err := bin.Open()
	if err != nil {
		return err
	}
	defer r.Close()

	filename := filepath.Join(destDir, binName)
	w, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
	if err != nil {
		return err
	}
	defer w.Close()

	// Copy nw binary
	_, err = io.Copy(w, r)
	if err != nil {
		return err
	}

	// Copy nw
	_, err = io.Copy(w, nw)
	if err != nil {
		return err
	}

	return nil
}
func cloneZipItem(f *zip.File, dest string) error {
	// Create full directory path
	path := filepath.Join(dest, f.Name)
	fmt.Println("Creating", path)
	err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm)
	if err != nil {
		return err
	}

	// Clone if item is a file
	rc, err := f.Open()
	if err != nil {
		return err
	}
	if !f.FileInfo().IsDir() {
		// Use os.Create() since Zip don't store file permissions.
		fileCopy, err := os.Create(path)
		if err != nil {
			return err
		}
		_, err = io.Copy(fileCopy, rc)
		fileCopy.Close()
		if err != nil {
			return err
		}
	}
	rc.Close()
	return nil
}
Example #17
0
func createFile(filename string, zipFile *zip.File, flag int) error {
	if filename == "" {
		return nil
	}
	var writer *os.File
	var err error
	if flag == 0 {
		writer, err = os.Create("files/books/" + filename)
	} else {
		i++
		writer, err = os.Create("files/books/pictures/" + strconv.Itoa(i) + filename)
	}
	if err != nil {
		return err
	}
	defer writer.Close()
	reader, err := zipFile.Open()
	if err != nil {
		return err
	}
	defer reader.Close()
	if _, err = io.Copy(writer, reader); err != nil {
		return err
	}
	return nil
}
Example #18
0
func findAndRead(file string, r *zip.ReadCloser) ([]byte, error) {
	var cont *zip.File
	//Searching for file
	for _, f := range r.File {
		if f.FileHeader.Name == file {
			cont = f
			break
		}
	}
	if cont == nil {
		return nil, errors.New(file + " not found")
	}
	//Opening and reading file
	reader, err := cont.Open()
	if err != nil {
		return nil, err
	}
	data, err := ioutil.ReadAll(reader)
	if err != nil {
		return nil, err
	}
	if reader.Close() != nil {
		return nil, err
	}
	return data, nil
}
Example #19
0
func CopyZipFile(f *zip.File, d, p string) error {
	if p != "" {
		dirname := path.Dir(p)
		if dirname != "." {
			err := os.MkdirAll(path.Join(d, dirname), 0755)
			if err != nil {
				return err
			}
		}
		rc, err := f.Open()
		defer rc.Close()
		if err != nil {
			return err
		}
		path := path.Join(d, p)
		stat, err := os.Stat(path)
		if err != nil || !stat.IsDir() {
			file, err := fs.Filesystem().OpenFile(path, os.O_WRONLY|os.O_CREATE, 0755)
			if err != nil {
				return err
			}
			defer file.Close()
			_, err = io.Copy(file, rc)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
Example #20
0
func TestOpenFileId(t *testing.T) {
	f, _ := Open(bookPath)
	defer f.Close()

	html, err := f.OpenFileId(fileId)
	if err != nil {
		t.Errorf("OpenFileId(%v) return an error: %v", fileId, err)
		return
	}
	defer html.Close()

	zipFile, _ := zip.OpenReader(bookPath)
	defer zipFile.Close()
	var file *zip.File
	for _, file = range zipFile.Reader.File {
		if file.Name == htmlPath {
			break
		}
	}
	zipHTML, _ := file.Open()

	buff1, err := ioutil.ReadAll(html)
	if err != nil {
		t.Errorf("Error reading the opened file: %v", err)
		return
	}
	buff2, _ := ioutil.ReadAll(zipHTML)
	if !bytes.Equal(buff1, buff2) {
		t.Errorf("The files on zip and OpenFile are not equal")
		return
	}
}
Example #21
0
// extractFile extracts zip.File to file system.
func extractFile(f *zip.File, destPath string) error {
	filePath := path.Join(destPath, f.Name)
	os.MkdirAll(path.Dir(filePath), os.ModePerm)

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

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

	if _, err = io.Copy(fw, rc); err != nil {
		return err
	}

	// Skip symbolic links.
	if f.FileInfo().Mode()&os.ModeSymlink != 0 {
		return nil
	}
	// Set back file information.
	if err = os.Chtimes(filePath, f.ModTime(), f.ModTime()); err != nil {
		return err
	}
	return os.Chmod(filePath, f.FileInfo().Mode())
}
Example #22
0
func parseIpaFile(plistFile *zip.File) (*BundleInfo, error) {
	if plistFile == nil {
		return nil, errors.New("info.plist is not found")
	}

	rc, err := plistFile.Open()
	if err != nil {
		return nil, err
	}
	defer rc.Close()

	buf, err := ioutil.ReadAll(rc)
	if err != nil {
		return nil, err
	}

	info := &iosInfo{}
	_, err = plist.Unmarshal(buf, info)
	if err != nil {
		return nil, err
	}

	bundleInfo := &BundleInfo{}
	bundleInfo.Version = info.CFBundleVersion
	bundleInfo.Identifier = info.CFBundleIdentifier
	bundleInfo.PlatformType = BundlePlatformTypeIOS

	return bundleInfo, nil
}
Example #23
0
func parseIPA(path string) (plinfo *plistBundle, err error) {
	plistre := regexp.MustCompile(`^Payload/[^/]*/Info\.plist$`)
	r, err := zip.OpenReader(path)
	if err != nil {
		return
	}
	defer r.Close()

	var plfile *zip.File
	for _, file := range r.File {
		if plistre.MatchString(file.Name) {
			plfile = file
			break
		}
	}
	if plfile == nil {
		err = errors.New("Info.plist file not found")
		return
	}
	plreader, err := plfile.Open()
	if err != nil {
		return
	}
	defer plreader.Close()
	buf := make([]byte, plfile.FileInfo().Size())
	_, err = io.ReadFull(plreader, buf)
	if err != nil {
		return
	}
	dec := goplist.NewDecoder(bytes.NewReader(buf))
	plinfo = new(plistBundle)
	err = dec.Decode(plinfo)
	return
}
Example #24
0
func parseAndroidManifest(xmlFile *zip.File) (*androidManifest, error) {
	rc, err := xmlFile.Open()
	if err != nil {
		return nil, err
	}
	defer rc.Close()

	buf, err := ioutil.ReadAll(rc)
	if err != nil {
		return nil, err
	}

	xmlContent, err := androidbinary.NewXMLFile(bytes.NewReader(buf))
	if err != nil {
		return nil, err
	}

	decoder := xml.NewDecoder(xmlContent.Reader())
	manifest := &androidManifest{}
	if err := decoder.Decode(manifest); err != nil {
		return nil, err
	}

	return manifest, nil
}
Example #25
0
// readSharedStringsFromZipFile() is an internal helper function to
// extract a reference table from the sharedStrings.xml file within
// the XLSX zip file.
func readSharedStringsFromZipFile(f *zip.File) (*RefTable, error) {
	var sst *xlsxSST
	var error error
	var rc io.ReadCloser
	var decoder *xml.Decoder
	var reftable *RefTable

	// In a file with no strings it's possible that
	// sharedStrings.xml doesn't exist.  In this case the value
	// passed as f will be nil.
	if f == nil {
		return nil, nil
	}
	rc, error = f.Open()
	if error != nil {
		return nil, error
	}
	sst = new(xlsxSST)
	decoder = xml.NewDecoder(rc)
	error = decoder.Decode(sst)
	if error != nil {
		return nil, error
	}
	reftable = MakeSharedStringRefTable(sst)
	return reftable, nil
}
Example #26
0
func extractFile(root string, file *zip.File) error {
	fmt.Printf("  extracting %s ...\n", file.Name)
	path := filepath.Join(root, file.Name)
	if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
		return err
	}
	r, err := file.Open()
	if err != nil {
		return err
	}
	perms := file.FileInfo().Mode().Perm()
	f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, perms)
	if err != nil {
		r.Close()
		return err
	}
	if _, err := io.Copy(f, r); err != nil {
		r.Close()
		f.Close()
		return err
	}
	if err := f.Close(); err != nil {
		return err
	}
	return r.Close()
}
Example #27
0
// readSheetsFromZipFile is an internal helper function that loops
// over the Worksheets defined in the XSLXWorkbook and loads them into
// Sheet objects stored in the Sheets slice of a xlsx.File struct.
func readSheetsFromZipFile(f *zip.File, file *File, sheetXMLMap map[string]string) ([]*Sheet, []string, error) {
	var workbook *xlsxWorkbook
	var error error
	var rc io.ReadCloser
	var decoder *xml.Decoder
	var sheetCount int
	workbook = new(xlsxWorkbook)
	rc, error = f.Open()
	if error != nil {
		return nil, nil, error
	}
	decoder = xml.NewDecoder(rc)
	error = decoder.Decode(workbook)
	if error != nil {
		return nil, nil, error
	}
	sheetCount = len(workbook.Sheets.Sheet)
	sheets := make([]*Sheet, sheetCount)
	names := make([]string, sheetCount)
	sheetChan := make(chan *indexedSheet, sheetCount)
	for i, rawsheet := range workbook.Sheets.Sheet {
		go readSheetFromFile(sheetChan, i, rawsheet, file, sheetXMLMap)
	}
	for j := 0; j < sheetCount; j++ {
		sheet := <-sheetChan
		if sheet.Error != nil {
			return nil, nil, sheet.Error
		}
		sheets[sheet.Index] = sheet.Sheet
		names[sheet.Index] = workbook.Sheets.Sheet[sheet.Index].Name
	}
	return sheets, names, nil
}
Example #28
0
func getMeta(f *zip.File, m map[string][]meta) {
	filename := f.FileHeader.Name

	var thisJSON []meta

	rc, err := f.Open()
	if err != nil {
		panic(err)
	}
	defer rc.Close()

	decoder := json.NewDecoder(rc)
	for decoder.More() {
		if err := decoder.Decode(&thisJSON); err != nil {
			panic(err)
		}
	}

	switch filename {
	case "users.json":
		m["users"] = thisJSON
	case "channels.json":
		m["channels"] = thisJSON
	default:
		panic(filename)
	}

}
Example #29
0
File: lib.go Project: 06linux/xlsx
// readSheetsFromZipFile is an internal helper function that loops
// over the Worksheets defined in the XSLXWorkbook and loads them into
// Sheet objects stored in the Sheets slice of a xlsx.File struct.
func readSheetsFromZipFile(f *zip.File, file *File) ([]*Sheet, []string, error) {
	var workbook *xlsxWorkbook
	var error error
	var rc io.ReadCloser
	var decoder *xml.Decoder
	workbook = new(xlsxWorkbook)
	rc, error = f.Open()
	if error != nil {
		return nil, nil, error
	}
	decoder = xml.NewDecoder(rc)
	error = decoder.Decode(workbook)
	if error != nil {
		return nil, nil, error
	}
	sheets := make([]*Sheet, len(workbook.Sheets.Sheet))
	names := make([]string, len(workbook.Sheets.Sheet))
	for i, rawsheet := range workbook.Sheets.Sheet {
		worksheet, error := getWorksheetFromSheet(rawsheet, file.worksheets)
		if error != nil {
			return nil, nil, error
		}
		sheet := new(Sheet)
		sheet.Rows, sheet.MaxCol, sheet.MaxRow = readRowsFromSheet(worksheet, file)
		sheets[i] = sheet
		names[i] = rawsheet.Name
	}
	return sheets, names, nil
}
Example #30
0
func writeExtensionFile(f *zip.File, extPath string) error {
	weferr := "write extension failed: "
	rc, err := f.Open()
	if err != nil {
		return errors.New(weferr + err.Error())
	}
	defer rc.Close()
	filename := filepath.Join(extPath, f.Name)
	if f.FileInfo().IsDir() {
		err = os.Mkdir(filename, 0770)
		if err != nil {
			return err
		}
	} else {
		dst, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
		if err != nil {
			return errors.New(weferr + err.Error())
		}
		defer dst.Close()
		_, err = io.Copy(dst, rc)
		if err != nil {
			return errors.New(weferr + err.Error())
		}
	}
	return nil
}