unzip

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

README

unzip

支持远程指定zip文件读取,无需手动解压或下载整个文件
  1. 打印本地/远端 ZIP 文件目录
  2. 通过文件名本地/远程读取指定文件
  3. 通过完整路径+文件名读取本地/远程指定文件
  4. 通过完整路径+文件名下载远程指定文件到本地
  5. todo:更多功能持续更新中
Install
go get github.com/go-pay/unzip
使用示例
package main

import (
    "context"
    "fmt"

    "github.com/go-pay/unzip"
)

func main() {
    c := context.Background()
    zipUrl := "https://tangboedu-1010.oss-cn-hangzhou.aliyuncs.com/remoteFile.zip"
    // 从远端读取指定文件
    zr, err := unzip.NewZipReader(c, zipUrl)
    if err != nil {
      fmt.Println(err)
    }
    fileStream, err := zr.ReadFileByPath(c, "/remoteFile/level1/level2/level3/version3.txt")
    if err != nil {
      fmt.Println(err)
    }
    fileContent := string(fileStream)
    fmt.Println(fileContent)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrHead               = errors.New("Accept-Ranges is not bytes")
	ErrZipReader          = errors.New("zip reader is nil")
	ErrZipReaderDirectory = errors.New("zip reader directory is nil")
	NotFoundZipFile       = errors.New("not found zip file")
)

Functions

func DecompressFile

func DecompressFile()

DecompressFile 解压指定文件

func DownloadRemoteFile

func DownloadRemoteFile(c context.Context, zipUrl string, files []string, saveDir ...string) (err error)

DownloadRemoteFile 远程解压指定文件 files: 需要解压的文件(完整路径)

Types

type CentralDirFileHead

type CentralDirFileHead struct {
	Signature        uint32 // 头标识符 (0x04034b50(大端))
	Version          uint16 // 版本
	NeedVersion      uint16 // 提取需要的版本
	Flag             uint16 // 通用位标志
	Method           uint16 // 压缩方法
	LastModTime      uint16 // 最后修改文件时间 时分秒
	LastModDate      uint16 // 最后修改文件日期 年月日
	Crc32            uint32 // crc-32(对压缩前的文件计算)
	CompressedSize   uint32 // 压缩大小
	UncompressedSize uint32 // 压缩前大小
	FileNameLen      uint16 // 文件名长度
	ExtraLen         uint16 // 额外字段长度
	CommentLen       uint16 // 文件注释长度
	DiskNumberStart  uint16 // 磁盘号
	InternalAttr     uint16 // 内部文件属性
	ExternalAttr     uint32 // 外部文件属性
	HeaderOffset     uint32 // 本地头的相对偏移量 (对应的本地文件相对于文件开始的偏移量)

	FileName string // 文件名
}

CentralDirFileHead 中央目录文件头(Central Directory File Header) /** * 中央目录文件头 * * central file header signature 4 bytes 中央文件头标识符 (0x02014b50(大端)) * version made by 2 bytes 版本 * version needed to extract 2 bytes 提取所需的版本 * general purpose bit flag 2 bytes 通用位标志 * compression method 2 bytes 压缩方法 * last mod file time 2 bytes 最后修改文件时间 时分秒 * last mod file date 2 bytes 最后修改文件日期 年月日 * crc-32 4 bytes crc-32 * compressed size 4 bytes 压缩大小 * uncompressed size 4 bytes 压缩前大小 * file name length 2 bytes 文件名长度 * extra field length 2 bytes 额外字段长度 * file comment length 2 bytes 文件注释长度 * disk number start 2 bytes 磁盘号 * internal file attributes 2 bytes 内部文件属性 * external file attributes 4 bytes 外部文件属性 * relative offset of local header 4 bytes 本地头的相对偏移量 (对应的本地文件相对于文件开始的偏移量) * * file name (variable size) 文件名 * extra field (variable size) 额外字段 * file comment (variable size) 文件注释 */ format "IHHHHIIIIHHHHHHII", []string{"I", "H", "H", "H", "H", "I", "I", "I", "I", "H", "H", "H", "H", "H", "I", "I"}

type ExtractFile

type ExtractFile struct {
	FileName         string
	Method           uint16
	CompressedSize   int64
	UncompressedSize int64
	HeaderOffset     int64
	RangeStart       int64
	RangeEnd         int64
}

type FileNode

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

type LocalFileHead

type LocalFileHead struct {
	Signature        uint32 // 本地文件头标识符 (0x04034b50(大端))
	NeedVersion      uint16 // 提取需要的版本
	Flag             uint16 // 通用位标志
	Method           uint16 // 压缩方法
	LastModTime      uint16 // 最后修改文件时间 时分秒
	LastModDate      uint16 // 最后修改文件日期 年月日
	Crc32            uint32 // crc-32(对压缩前的文件计算)
	CompressedSize   uint32 // 压缩大小
	UncompressedSize uint32 // 未压缩大小
	FileNameLen      uint16 // 文件名长度
	ExtraLen         uint16 // 额外字段长度

	FileName string // 文件名
}

LocalFileHead 本地文件头组成(Local File Header) /** * 本地文件头组成 * * local file header signature 4 bytes 本地文件头标识符 (0x04034b50(大端)) * version needed to extract 2 bytes 提取需要的版本 * general purpose bit flag 2 bytes 通用位标志 * compression method 2 bytes 压缩方法 * last mod file time 2 bytes 最后修改文件时间 时分秒 * last mod file date 2 bytes 最后修改文件日期 年月日 * crc-32 4 bytes crc-32(对压缩前的文件计算) * compressed size 4 bytes 压缩大小 * uncompressed size 4 bytes 未压缩大小 * file name length 2 bytes 文件名长度 * extra field length 2 bytes 额外字段长度 * file name (variable size) 文件名 * extra field (variable size) 额外字段 */ format: "IHHHIIIIHH", []string{"I", "H", "H", "H", "I", "I", "I", "I", "H", "H"}

type ZipReader

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

func NewZipReader

func NewZipReader(c context.Context, zipUrl string) (zr *ZipReader, err error)

NewZipReader 输入远端zip下载地址或者本地zip包路径

func (*ZipReader) DownloadRemoteFile

func (zr *ZipReader) DownloadRemoteFile(c context.Context, files []string, saveDir ...string) (err error)

DownloadRemoteFile 远程解压指定文件

func (*ZipReader) PrintDirectory

func (zr *ZipReader) PrintDirectory() error

PrintDirectory 打印远端zip文件目录

func (*ZipReader) ReadFileByName

func (zr *ZipReader) ReadFileByName(c context.Context, files []string) (fileContentMap map[string][]byte, err error)

ReadFileByName todo:多个文件并发操作 通过文件名远程读取指定文件,返回key:path value:fileContent(可能存在同名,以不同key:path区分)

func (*ZipReader) ReadFileByPath

func (zr *ZipReader) ReadFileByPath(c context.Context, filePath string) (fileContent []byte, err error)

ReadFileByPath 通过完整路径+文件名远程读取指定文件

Directories

Path Synopsis
Package zip provides support for reading and writing ZIP archives.
Package zip provides support for reading and writing ZIP archives.

Jump to

Keyboard shortcuts

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