예제 #1
0
func TestZipWriter(t *testing.T) {
	// Have to write an actual file for zip.OpenReader to use later.
	f, err := ioutil.TempFile("", "zip_writer_test")
	if err != nil {
		t.Fatalf("Failed to create temp file: %s", err)
	}
	filename := f.Name()
	defer os.Remove(filename)
	w := zip.NewWriter(f)
	if err := AddZipFile(w, "src/build/java/test_data/test.zip", nil, nil, "", true, nil); err != nil {
		t.Fatalf("Failed to add zip file: %s", err)
	}
	if err := w.Close(); err != nil {
		t.Fatalf("Failed to close zip file: %s", err)
	}
	w.Close()
	f.Close()

	r, err := zip.OpenReader(filename)
	if err != nil {
		t.Fatalf("Failed to reopen zip file: %s", err)
	}
	defer r.Close()

	files := []struct{ Name, Prefix string }{
		{"build_step.go", "// Implementation of Step interface."},
		{"incrementality.go", "// Utilities to help with incremental builds."},
	}
	for i, f := range r.File {
		if f.Name != files[i].Name {
			t.Errorf("File %d has wrong name: expected %s, was %s", i, files[i].Name, f.Name)
		}
		fr, err := f.Open()
		if err != nil {
			t.Errorf("Failed to reopen file %d [%s]: %s", i, f.Name, err)
		} else {
			buf := new(bytes.Buffer)
			if _, err = io.Copy(buf, fr); err != nil {
				t.Errorf("Failed to read full contents of file %d [%s]: %s", i, f.Name, err)
			} else if !strings.HasPrefix(buf.String(), files[i].Prefix) {
				t.Errorf("File %d [%s] didn't start with expected prefix: was %s", buf.String()[:20])
			}
			fr.Close()
		}
	}
}
예제 #2
0
func combine(out, in, preamble, stripPrefix, mainClass, excludeInternalPrefix string,
	excludeSuffixes, suffix, includeInternalPrefixes []string,
	strict, includeOther, addInitPy, dirEntries bool, renameDirs map[string]string) error {
	f, err := os.Create(out)
	if err != nil {
		return err
	}
	defer f.Close()

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

	if preamble != "" {
		if err := w.WritePreamble(preamble + "\n"); err != nil {
			return nil
		}
	}

	excludeInternalPrefixes := strings.Split(excludeInternalPrefix, ",")
	if excludeInternalPrefix == "" {
		excludeInternalPrefixes = []string{}
	}

	var walkFunc func(path string, info os.FileInfo, err error) error
	walkFunc = func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		} else if path != in && (info.Mode()&os.ModeSymlink) != 0 {
			if resolved, err := filepath.EvalSymlinks(path); err != nil {
				return err
			} else if stat, err := os.Stat(resolved); err != nil {
				return err
			} else if stat.IsDir() {
				return filepath.Walk(resolved, walkFunc)
			}
		}
		if path == out {
			return nil
		} else if !info.IsDir() {
			if !matchesSuffix(path, excludeSuffixes) {
				if matchesSuffix(path, suffix) {
					log.Debug("Adding zip file %s", path)
					if err := java.AddZipFile(w, path, includeInternalPrefixes, excludeInternalPrefixes, stripPrefix, strict, renameDirs); err != nil {
						return fmt.Errorf("Error adding %s to zipfile: %s", path, err)
					}
				} else if includeOther && !java.HasExistingFile(w, path) {
					log.Debug("Including existing non-zip file %s", path)
					if b, err := ioutil.ReadFile(path); err != nil {
						return fmt.Errorf("Error reading %s to zipfile: %s", path, err)
					} else if err = java.WriteFile(w, path, b); err != nil {
						return err
					}
				}
			}
		} else if (len(suffix) == 0 || addInitPy) && path != "." && dirEntries { // Only add directory entries in "dumb" mode.
			log.Debug("Adding directory entry %s/", path)
			if err := java.WriteDir(w, path); err != nil {
				return err
			}
		}
		return nil
	}
	if err := filepath.Walk(in, walkFunc); err != nil {
		return err
	}
	if mainClass != "" {
		if err := java.AddManifest(w, mainClass); err != nil {
			return err
		}
	}
	if addInitPy {
		return java.AddInitPyFiles(w)
	}
	return nil
}