compress

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

compress: a go compress library for fs.FS interface

Format Test Charset Decoder Encoder Password Info
zip local true true true false used go std
rar local false true false true rardecode/v2
7zip false false true false true not work in big file(>10M)
github.com/ulikunitz/xz/lzma.(*rangeDecoder).DecodeBit too slow

use

go get github.com/pashifika/compress

Example:

package main

import (
	"bytes"
	"fmt"
	"io"
	"io/fs"
	"log"

	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/japanese"

	"github.com/pashifika/compress"
	_ "github.com/pashifika/compress/rar"
	_ "github.com/pashifika/compress/zip"
)

func main() {
	// set charset to decode zip header name
	fsys := &compress.FileSystem{
		Charset:     []encoding.Encoding{japanese.ShiftJIS},
		SkipCharErr: false,
	}
	path := "you test archive file path"
	rc, err := fsys.Open(path)
	if err != nil {
		log.Fatal(err)
	}
	defer fsys.Close()

	err = fs.WalkDir(rc, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if d.IsDir() {
			switch path {
			case ".git":
				return fs.SkipDir
			case compress.DefaultArchiverRoot:
				return nil
			default:
				fmt.Println("dir:", path)
				return nil
			}
		}
		if !d.IsDir() {
			af, err := rc.Open(path)
			if err != nil {
				panic(err)
			}
			buf := bytes.Buffer{}
			n, err := io.Copy(&buf, af)
			if err != nil {
				panic(err)
			}
			fmt.Println("file:", path)
			fmt.Println(n, err, n, buf.Len())
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}
}

Documentation

Overview

Package compress

  • Version: 1.0.0
  • Copyright (c) 2022. Pashifika *
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at *
  • http://www.apache.org/licenses/LICENSE-2.0 *
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.

Package compress

  • Version: 1.0.0
  • Copyright (c) 2022. Pashifika *
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at *
  • http://www.apache.org/licenses/LICENSE-2.0 *
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.

Package compress

  • Version: 1.0.0
  • Copyright (c) 2022. Pashifika *
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at *
  • http://www.apache.org/licenses/LICENSE-2.0 *
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.

Package compress

  • Version: 1.0.0
  • Copyright (c) 2022. Pashifika *
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at *
  • http://www.apache.org/licenses/LICENSE-2.0 *
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.

Package compress

  • Version: 1.0.0
  • Copyright (c) 2022. Pashifika *
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at *
  • http://www.apache.org/licenses/LICENSE-2.0 *
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.

Index

Constants

View Source
const DefaultArchiverRoot = "."

Variables

View Source
var (
	ErrUnknownEncoder   = errors.New("unknown encoder")
	ErrUnknownArchiver  = errors.New("unknown archiver file")
	ErrWriterNotSupport = errors.New("writer is not supported")

	// ErrDirIndexTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
	ErrDirIndexTooLarge = errors.New("DirIndex.slice: too large")
)

Functions

func RegisterDecoder

func RegisterDecoder(decoder Decoder)

RegisterDecoder registers decoder. It should be called during init. Duplicate decoders by name are not allowed and will panic.

func RegisterEncoder

func RegisterEncoder(encode Encoder)

RegisterEncoder registers encoder. It should be called during init. Duplicate decoders by name are not allowed and will panic.

func Version

func Version() string

Types

type ArchiverFile

type ArchiverFile interface {
	fs.FileInfo
	fs.ReadDirFile
	fs.DirEntry
	io.Writer

	Root() string
}

type Decoder

type Decoder interface {
	// Name is get Decoder name.
	Name() string

	// SetRootInfo is set archiver file info to Decoder.
	SetRootInfo(info os.FileInfo)

	// SetCharset option support(only work in zip).
	SetCharset(charset []encoding.Encoding, skipErr bool)

	// OpenReader open the archive specified by name and returns io.ReadCloser.
	//
	// * 7z / rar is support part files.
	OpenReader(path string) (fs.FS, error)

	// OpenReaderWithPassword will open the archive file specified by name using password as
	// the basis of the decryption key and returns io.ReadCloser.
	//
	// * 7z / rar is support part files.
	OpenReaderWithPassword(path, pwd string) (fs.FS, error)

	// GetDirEntries get the archive path entries (if you know).
	GetDirEntries(path string, n int) ([]fs.DirEntry, error)

	// Close closes the archive file, rendering it unusable for I/O.
	Close() error

	// Reset is reset Decoder memory cache.
	Reset()
}

type DirIndex

type DirIndex struct {
	// contains filtered or unexported fields
}

func NewDirEntries

func NewDirEntries() *DirIndex

func (*DirIndex) Add

func (d *DirIndex) Add(idx int)

func (*DirIndex) Entries

func (d *DirIndex) Entries() []int

func (*DirIndex) Len

func (d *DirIndex) Len() int

Len returns the number of slice of the unread portion of the DirIndex;

func (*DirIndex) Reset

func (d *DirIndex) Reset()

Reset resets the DirIndex to be empty

type Encoder

type Encoder interface {
	// Name is get Encoder name.
	Name() string

	SetCompressedExt(ext map[string]struct{})

	// Create use encoder name to create new archive file
	Create(w io.Writer, entries []ArchiverFile) error

	// Close closes the archive file, writing it unusable for I/O.
	Close() error

	// Reset is reset Encoder memory cache.
	Reset()
}

type FileSystem

type FileSystem struct {
	Charset     []encoding.Encoding
	SkipCharErr bool
	// contains filtered or unexported fields
}

func (*FileSystem) Close

func (fs *FileSystem) Close() error

func (*FileSystem) CreateArchiverFile

func (fs *FileSystem) CreateArchiverFile(encode string, w io.Writer, entries []ArchiverFile) error

CreateArchiverFile save archiver entries to disk

func (*FileSystem) Open

func (fs *FileSystem) Open(path string) (fs.FS, error)

Open cannot work with OpenWithPwd at the same time

func (*FileSystem) OpenWithPwd

func (fs *FileSystem) OpenWithPwd(path, pwd string) (fs.FS, error)

OpenWithPwd cannot work with Open at the same time

Directories

Path Synopsis
Package _7zip * Version: 1.0.0 * Copyright (c) 2022.
Package _7zip * Version: 1.0.0 * Copyright (c) 2022.
internal
std_zip
Package std_zip * Version: 1.0.0 * Copyright (c) 2022.
Package std_zip * Version: 1.0.0 * Copyright (c) 2022.
Package rar * Version: 1.0.0 * Copyright (c) 2022.
Package rar * Version: 1.0.0 * Copyright (c) 2022.
Package zip * Version: 1.0.0 * Copyright (c) 2022.
Package zip * Version: 1.0.0 * Copyright (c) 2022.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL