Skip to content

Added GUnzip function #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions _testdata/test.txt
Original file line number Diff line number Diff line change
@@ -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.

Binary file added _testdata/test.txt.gz
Binary file not shown.
70 changes: 70 additions & 0 deletions gzip.go
Original file line number Diff line number Diff line change
@@ -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
}
58 changes: 58 additions & 0 deletions gzip_test.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 12 additions & 4 deletions paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -275,19 +275,23 @@ 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) {
testPath := New("_testdata")

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)
Expand All @@ -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) {
Expand Down