diff --git a/_testdata/test.txt b/_testdata/test.txt new file mode 100644 index 0000000..d3ded99 --- /dev/null +++ b/_testdata/test.txt @@ -0,0 +1,20 @@ +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + diff --git a/_testdata/test.txt.gz b/_testdata/test.txt.gz new file mode 100644 index 0000000..e75120a Binary files /dev/null and b/_testdata/test.txt.gz differ diff --git a/gzip.go b/gzip.go new file mode 100644 index 0000000..3a841ae --- /dev/null +++ b/gzip.go @@ -0,0 +1,70 @@ +/* + * This file is part of PathsHelper library. + * + * Copyright 2021 Arduino AG (http://www.arduino.cc/) + * + * PathsHelper library is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ + +package paths + +import ( + "compress/gzip" + "io" + + "github.com/pkg/errors" +) + +// GZip compress src with gzip and writes the compressed file on dst +//func GZip(src, dst *Path) error { +// return errors.New("gzip unimplemented") +//} + +// GUnzip decompress src with gzip and writes the uncompressed file on dst +func GUnzip(src, dest *Path) error { + gzIn, err := src.Open() + if err != nil { + return errors.Wrap(err, "opening "+src.String()) + } + defer gzIn.Close() + + in, err := gzip.NewReader(gzIn) + if err != nil { + return errors.Wrap(err, "decoding "+src.String()) + } + defer in.Close() + + out, err := dest.Create() + if err != nil { + return errors.Wrap(err, "creating "+dest.String()) + } + defer out.Close() + + _, err = io.Copy(out, in) + if err != nil { + return errors.Wrap(err, "uncompressing "+dest.String()) + } + + return nil +} diff --git a/gzip_test.go b/gzip_test.go new file mode 100644 index 0000000..d102488 --- /dev/null +++ b/gzip_test.go @@ -0,0 +1,58 @@ +/* + * This file is part of PathsHelper library. + * + * Copyright 2021 Arduino AG (http://www.arduino.cc/) + * + * PathsHelper library is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ + +package paths + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestGzipGunzip(t *testing.T) { + zipped := New("_testdata", "test.txt.gz") + unzipped := New("_testdata", "test.txt") + + tmp, err := MkTempDir("", "") + require.NoError(t, err) + defer tmp.RemoveAll() + + // Test decoding + decoded := tmp.Join("test") + err = GUnzip(zipped, decoded) + require.NoError(t, err) + d, err := decoded.ReadFile() + require.NoError(t, err) + u, err := unzipped.ReadFile() + require.NoError(t, err) + require.Equal(t, u, d) + + // Test encoding + // TODO +} diff --git a/paths_test.go b/paths_test.go index 09ba8f2..c3230d5 100644 --- a/paths_test.go +++ b/paths_test.go @@ -259,7 +259,7 @@ func TestReadDirRecursive(t *testing.T) { list, err := testPath.ReadDirRecursive() require.NoError(t, err) - require.Len(t, list, 14) + require.Len(t, list, 16) pathEqualsTo(t, "_testdata/anotherFile", list[0]) pathEqualsTo(t, "_testdata/file", list[1]) @@ -275,6 +275,8 @@ func TestReadDirRecursive(t *testing.T) { pathEqualsTo(t, "_testdata/symlinktofolder/file3", list[11]) pathEqualsTo(t, "_testdata/symlinktofolder/subfolder", list[12]) pathEqualsTo(t, "_testdata/symlinktofolder/subfolder/file4", list[13]) + pathEqualsTo(t, "_testdata/test.txt", list[14]) + pathEqualsTo(t, "_testdata/test.txt.gz", list[15]) } func TestFilterDirs(t *testing.T) { @@ -282,12 +284,14 @@ func TestFilterDirs(t *testing.T) { list, err := testPath.ReadDir() require.NoError(t, err) - require.Len(t, list, 4) + require.Len(t, list, 6) pathEqualsTo(t, "_testdata/anotherFile", list[0]) pathEqualsTo(t, "_testdata/file", list[1]) pathEqualsTo(t, "_testdata/folder", list[2]) pathEqualsTo(t, "_testdata/symlinktofolder", list[3]) + pathEqualsTo(t, "_testdata/test.txt", list[4]) + pathEqualsTo(t, "_testdata/test.txt.gz", list[5]) list.FilterDirs() require.Len(t, list, 2) @@ -300,17 +304,21 @@ func TestFilterOutDirs(t *testing.T) { list, err := testPath.ReadDir() require.NoError(t, err) - require.Len(t, list, 4) + require.Len(t, list, 6) pathEqualsTo(t, "_testdata/anotherFile", list[0]) pathEqualsTo(t, "_testdata/file", list[1]) pathEqualsTo(t, "_testdata/folder", list[2]) pathEqualsTo(t, "_testdata/symlinktofolder", list[3]) + pathEqualsTo(t, "_testdata/test.txt", list[4]) + pathEqualsTo(t, "_testdata/test.txt.gz", list[5]) list.FilterOutDirs() - require.Len(t, list, 2) + require.Len(t, list, 4) pathEqualsTo(t, "_testdata/anotherFile", list[0]) pathEqualsTo(t, "_testdata/file", list[1]) + pathEqualsTo(t, "_testdata/test.txt", list[2]) + pathEqualsTo(t, "_testdata/test.txt.gz", list[3]) } func TestEquivalentPaths(t *testing.T) {