14 Star 3 Fork 25

OpenHarmony-TPC / CommonsCompress

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

commons-compress

简介

本软件是参照开源软件 Apache Commons Compress 源码并用 TypeScript 语言实现了相关功能, 在OpenHarmony上支持bzip2、gzip、lzma、xz、Snappy、LZ4、Brotli、DEFLATE、Zstandard 和 ar、cpio、tar、zip、dump、7z等格式的压缩和解压功能。

compp.gif

下载

  ohpm install @ohos/commons-compress

OpenHarmony ohpm 环境配置等更多内容,请参考如何安装 OpenHarmony ohpm 包

使用说明

配置全局路径

需要在EntryAbility文件配置全局路径

GlobalContext.getContext().setObject("context", this.context);

文件存储路径说明

开发者在应用开发调试时,可能需要向应用沙箱下推送一些文件以期望在应用内访问或测试,可以参照推送文件说明文档

zip解压缩能力说明

本软件的zip解压缩能力示例是通过调用系统接口 @ohos.zlib 实现的,详细接口说明可以查看Zip模块

zip 压缩功能

指定文件夹路径压缩zip文件夹。

import fileio from '@ohos.fileio';
import zlib from '@ohos.zlib';

let filesDir = GlobalContext.getContext().getObject("FilesDir");

jsZipTest(): void {
    try {
      var data = filesDir
      let inFile = data + '/' + this.newFolder + '/hello.txt';
      let outFile = data + '/' + this.newFolder + '.zip';
      let options = {
        level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
        memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
        strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
      };

      zlib.compressFile(inFile, outFile, options, (errData) => {
        if (errData !== null) {
          console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
        } else {
          AlertDialog.show({ title: '压缩成功',
            message: '请查看沙箱路径 ' + data + '/',
            confirm: { value: 'OK', action: () => {
              this.isDeCompressGZipShow = true
            } }
          })
        }
      })
    } catch (error) {
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

zip 解压功能

指定文件夹路径解压zip文件夹。

import fileio from '@ohos.fileio';
import zlib from '@ohos.zlib';

let filesDir = GlobalContext.getContext().getObject("FilesDir");

unJsZipTest(): void {
    try {
      var data = filesDir
      let inFile = data + '/' + this.newFolder + '.zip';
      let outFile = data;
      let options = {
        level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
        memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
        strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
      };

      zlib.decompressFile(inFile, outFile, options, (errData) => {
        if (errData !== null) {
          console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
        }else {
          AlertDialog.show({ title: '解缩成功',
            message: '请查看沙箱路径 ' + data ,
            confirm: { value: 'OK', action: () => {
            } }
          })
        }
      })
    } catch (error) {
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }  

gzip 压缩功能

指定文件夹路径压缩gz文件夹。

import {gzipFile} from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  gzipFileTest(): void {
    try{
      var data = filesDir
        console.info('directory obtained. Data:' + data);
        gzipFile(data + '/hello.txt', data + '/test.txt.gz')
          .then((isSuccess) => {
            if (isSuccess) {
              AlertDialog.show({ title: '压缩成功',
                message: '请查看沙箱路径 ' + data + '/test.txt.gz',
                confirm: { value: 'OK', action: () => {
                  this.isDeCompressGZipShow = true
                } }
              })
            }
          });
    }catch(error){
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

gzip 解压功能

指定文件夹路径解压gz文件夹。

import {unGzipFile} from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  unGzipFileTest(): void {
    try{
      var data = filesDir
        unGzipFile(data + '/test.txt.gz', data + '/test.txt')
          .then((isSuccess) => {
            if (isSuccess) {
              AlertDialog.show({ title: '解缩成功',
                message: '请查看沙箱路径 ' + data + '/test.txt',
                confirm: { value: 'OK', action: () => {
                } }
              })
            }
          });
    }catch(error){
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

xz 压缩功能

指定文件夹路径压缩XZ文件夹。

import {File, OutputStream, InputStream, IOUtils, CompressorStreamFactory, CompressorOutputStream } from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  testXZCreation(): void {
    try{
      var data = filesDir
        console.info('directory obtained. Data:' + data);
        let input = new File(data, "/hello.txt");
        let output = new File(data, "/hello.txt.xz");
        let out: OutputStream = new OutputStream();
        let input1: InputStream = new InputStream();
        out.setFilePath(output.getPath());
        input1.setFilePath(input.getPath());
        let cos: CompressorOutputStream = new CompressorStreamFactory(false). createCompressorOutputStream("xz", out)
        IOUtils.copyStream(input1, cos);
        cos.close();
        input1.close()
        AlertDialog.show({ title: '压缩成功',
          message: '请查看沙箱路径 ' + data + '/hello.txt.xz',
          confirm: { value: 'OK', action: () => {
            this.isDeCompressBzip2Show = true
          } }
        })
    }catch(error){
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

xz 解压功能

指定文件夹路径解压XZ文件夹。

import {File, OutputStream, InputStream, IOUtils, CompressorStreamFactory, CompressorInputStream} from '@ohos/commons-compress' 
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  unXZFileTest(): void {
    try{
      var data = filesDir
        let input = new File(data, "/hello.txt.xz");
        let output = new File(data, "/hello1.txt");
        let out: OutputStream = new OutputStream();
        let input1: InputStream = new InputStream();
        out.setFilePath(output.getPath());
        input1.setFilePath(input.getPath());
        let inputs: CompressorInputStream = new CompressorStreamFactory(false). createCompressorNameInputStream("xz", input1)
        IOUtils.copyStream(inputs, out);
        out.close();
        input1.close();
        AlertDialog.show({ title: '解缩成功',
          message: '请查看沙箱路径 ' + data + '/hello1.txt',
          confirm: { value: 'OK', action: () => {
          } }
        })
    }catch(error){
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

Z 解压功能

指定文件夹路径解压Z文件夹。

import {File, OutputStream, InputStream, IOUtils, CompressorInputStream, CompressorStreamFactory} from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  unZFileTest(): void {
    try{
      var data = filesDir
        let inputStream: InputStream = new InputStream();
        inputStream.setFilePath(data + '/bla.tar.Z');
        let fOut: OutputStream = new OutputStream();
        fOut.setFilePath(data + '/bla.tar');
        let input: CompressorInputStream = new CompressorStreamFactory(false). createCompressorNameInputStream("z", inputStream);
        IOUtils.copyStream(input, fOut);
        inputStream.close();
        fOut.close();
        AlertDialog.show({ title: '解缩成功',
          message: '请查看沙箱路径 ' + data + '/bla.tar',
          confirm: { value: 'OK', action: () => {
          } }
        })
       }catch(error){
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

zstd 压缩功能

指定文件夹路径压缩zstd 文件夹。

import { ZstdCompress } from '@ohos/commons-compress';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
async testZstdCompressed(): Promise<void> {
    try {
        var data = filesDir
        await zstdCompress(data + "/hello.txt", data + "/hello.txt.zstd", 1, 3).then((value) => {
            if (value) {
                AlertDialog.show({ title: '压缩成功',
                    message: '请查看沙箱路径 ' + data + '/hello.txt.zstd',
                    confirm: { value: 'OK', action: () => {
                        this.isDeCompressZstdShow = true
                    } }
                })
            } else {
                AlertDialog.show({ title: '压缩失败',
                    message: '请检查再压缩 ',
                    confirm: { value: 'OK', action: () => {
                    } }
                })
            }
        })
    } catch (error) {
        console.error('File to obtain the file directory. Cause: ' + error.message);
    }
}

zstd 解压功能

指定文件夹路径解压zstd 文件夹。

import { ZstdDecompress } from '@ohos/commons-compress';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
async testZstdDecompressed(): Promise<void> {
    try {
        var data = filesDir
        await zstdDecompress(data + "/hello.txt.zstd", data + '/newhello.txt').then((value) => {
            if (value) {
                AlertDialog.show({ title: '解压成功',
                    message: '请查看沙箱路径 ' + data + '/newhello.txt',
                    confirm: { value: 'OK', action: () => {
                    } }
                })
            } else {
                AlertDialog.show({ title: '解压失败',
                    message: '请检查再解压',
                    confirm: { value: 'OK', action: () => {
                    } }
                })
            }
        })
    } catch (error) {
        console.error('File to obtain the file directory. Cause: ' + error.message);
    }
}

ar 压缩功能

指定文件夹路径压缩ar文件夹。

import { ArchiveEntry, ArchiveUtils, ArArchiveInputStream, ArArchiveEntry, ArchiveStreamFactory, ArchiveOutputStream, InputStream, File,OutputStream,IOUtils} from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");

  jsArTest(): void {
    try{
      var data = filesDir
        this.testArArchiveCreation(data)
    }catch(error){
      console.error(error.message);
    }
  }

  testArArchiveCreation(data: string) {
    let output: File = new File(data, this.newFolder + '.ar');
    let file1: File = new File(data + '/' + this.newFolder, 'test1.xml');

    let out: OutputStream = new OutputStream();
    let input1: InputStream = new InputStream();
    out.setFilePath(output.getPath());
    input1.setFilePath(file1.getPath());
    let os: ArchiveOutputStream = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream("ar", out, "");
    os.putArchiveEntry(new ArArchiveEntry("test1.xml", Long.fromNumber(file1.length()), 0, 0,
      ArArchiveEntry.DEFAULT_MODE, Long.fromNumber(new Date().getTime() / 1000)));
    IOUtils.copyStream(input1, os);
    os.closeArchiveEntry();
    os.close();
    AlertDialog.show({ title: '压缩成功',
      message: '请查看沙箱路径 ' + data + '/',
      confirm: { value: 'OK', action: () => {
        this.isDeCompressGArShow = true
      } }
    })
  }

ar 解压功能

指定文件夹路径解压ar文件夹。

import { ArchiveEntry, ArchiveUtils, ArArchiveInputStream, ArArchiveEntry, ArchiveStreamFactory, ArchiveOutputStream, InputStream, File,OutputStream,IOUtils} from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  jsUnArTest(): void {
    try{
      var data = filesDir
        this.testReadLongNamesBSD(data)
    }catch(error){
      console.error( error.message);
    }
  }

  testReadLongNamesBSD(data: string): void {
    let inFile: File = new File(data + '/' + this.newFolder, "longfile_bsd.ar");
    let input: InputStream = new InputStream();
    input.setFilePath(inFile.getPath());
    let s: ArArchiveInputStream = new ArArchiveInputStream(input);
    let tarArchiveEntry: ArchiveEntry = null;
    while ((tarArchiveEntry = s.getNextEntry()) != null) {
      let name: string = tarArchiveEntry.getName();
      let tarFile: File = new File(data + '/' + this.newFolder, name);
      if (name.indexOf('/') != -1) {
        try {
          let splitName: string = name.substring(0, name.lastIndexOf('/'));
          fileio.mkdirSync(data + '/' + this.newFolder + '/' + splitName);
        } catch (err) {
        }
      }
      let fos: OutputStream = null;
      try {
        fos = new OutputStream();
        fos.setFilePath(tarFile.getPath())
        let read: number = -1;
        let buffer: Int8Array = new Int8Array(1024);
        while ((read = s.readBytes(buffer)) != -1) {
          fos.writeBytesOffset(buffer, 0, read);
        }
      } catch (e) {
        throw e;
      } finally {
        fos.close();
      }
    }
    AlertDialog.show({ title: '解压成功',
      message: '请查看沙箱路径 ' + data + '/' + this.newFolder,
      confirm: { value: 'OK', action: () => {
      } }
    })
  }

brotli 解压功能

指定文件夹路径解压brotli文件夹。

import { CompressorInputStream,CompressorStreamFactory,ArchiveInputStream,ArchiveStreamFactory,ArchiveEntry,InputStream,File,OutputStream,IOUtils} from '@ohos/commons-compress'
import fileio from '@ohos.fileio';

let filesDir = GlobalContext.getContext().getObject("FilesDir");
  brotilTest(): void {
    try{
      var data = filesDir
        this.generateTextFile(data, '/bla.tar.br', compressData)

        let input = new File(data, "/bla.tar.br");
        let output = new File(data, "/bla.tar");
        let out: OutputStream = new OutputStream();
        let input1: InputStream = new InputStream();
        out.setFilePath(output.getPath());
        input1.setFilePath(input.getPath());
        let inputs: CompressorInputStream = new CompressorStreamFactory().createCompressorNameInputStream("br", input1)
        IOUtils.copyStream(inputs, out);
        out.close();
        input1.close();
        AlertDialog.show({ title: '解缩成功',
          message: '请查看沙箱路径 ' + data + '/bla.tar',
          confirm: { value: 'OK', action: () => {
          } }
        })
      }catch(error){
        console.error('File to obtain the file directory. Cause: ' + error.message);
      }
  }

  generateTextFile(data: string, fileName: string, arr: Int8Array | Int32Array): void {
    let srcPath = data;
    try {
      fileio.mkdirSync(srcPath);
    } catch (err) {
    }
    const writer = fileio.openSync(srcPath + fileName, 0o102, 0o666);
    fileio.writeSync(writer, arr.buffer);
    fileio.closeSync(writer);
  }

bzip2 压缩功能

指定文件夹路径压缩bzip2文件夹。

import { CompressorInputStream,CompressorStreamFactory,InputStream,OutputStream,IOUtils,CompressorOutputStream} from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  bzip2FileTest(): void {
    try{
      var data = filesDir
        console.info('directory obtained. Data:' + data);
        let inputStream: InputStream = new InputStream();
        inputStream.setFilePath(data + '/hello.txt');
        let fOut: OutputStream = new OutputStream();
        fOut.setFilePath(data + '/hello.txt.bz2');
        let cos: CompressorOutputStream = new CompressorStreamFactory(false).createCompressorOutputStream("bzip2", fOut);
        IOUtils.copyStream(inputStream, cos);
        cos.close();
        inputStream.close();
        AlertDialog.show({ title: '压缩成功',
          message: '请查看沙箱路径 ' + data + '/hello.txt.gz',
          confirm: { value: 'OK', action: () => {
            this.isDeCompressBzip2Show = true
          } }
        })
    }catch(error){
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

bzip2 解压功能

指定文件夹路径解压bzip2文件夹。

import { CompressorInputStream,CompressorStreamFactory,InputStream,OutputStream,IOUtils,CompressorOutputStream} from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  unBzip2FileTest(): void {
    try{
      var data = filesDir
        let inputStream: InputStream = new InputStream();
        inputStream.setFilePath(data + '/hello.txt.bz2');
        let fOut: OutputStream = new OutputStream();
        fOut.setFilePath(data + '/hello.txt');
        let input: CompressorInputStream = new CompressorStreamFactory(false).createCompressorNameInputStream("bzip2", inputStream);
        IOUtils.copyStream(input, fOut);
        inputStream.close();
        fOut.close();
        AlertDialog.show({ title: '解缩成功',
          message: '请查看沙箱路径 ' + data + '/hello.txt',
          confirm: { value: 'OK', action: () => {
          } }
        })
      }catch(error){
        console.error('File to obtain the file directory. Cause: ' + error.message);
      }
  }

lz4 压缩功能

指定文件夹路径压缩lz4文件夹。

 import {lz4Compressed} from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  lz4CompressedTest(): void {
    try{
      var data = filesDir
        let timer1 = System.currentTimeMillis().toString()
        console.info(this.TAG + timer1)

        lz4Compressed(data + '/bla.tar', data + '/bla.tar.lz4')

        let timer2: string = System.currentTimeMillis().toString()
        console.info(this.TAG + timer2)

        AlertDialog.show({ title: '压缩成功',
          message: '请查看沙箱路径 ' + data + '/test.txt.lz4',
          confirm: { value: 'OK', action: () => {
            this.isDeCompressLz4Show = true
          } }
        })
      }catch(error){
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

lz4 解压功能

指定文件夹路径解压lz4文件夹。

import {lz4Decompressed} from '@ohos/commons-compress'  
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");

  lz4DecompressedTest(): void {
    try{
      var data = filesDir

        let timer1 = System.currentTimeMillis().toString()
        console.info(this.TAG + timer1)

        lz4Decompressed(data + '/bla.tar.lz4-framed.lz4', data + '/bla.tar')

        let timer2: string = System.currentTimeMillis().toString()
        console.info(this.TAG + timer2)

        AlertDialog.show({ title: '解缩成功',
          message: '请查看沙箱路径 ' + data + '/test2.txt',
          confirm: { value: 'OK', action: () => {
          } }
        })
      }catch(error){
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

lzma,sevenz7 压缩功能

指定文件夹路径压缩lzma,sevenz7文件夹。

  import {Decoder, Encoder,InputStream,OutputStream,Exception,System } from '@ohos/commons-compress'
import fileio from '@ohos.fileio';

   encoder(path) {
    console.info(this.TAG + '开始')
    let inputStream: InputStream = new InputStream();
    let outputStream: OutputStream = new OutputStream();
    outputStream.setFilePath(path + '/test.xml.lzma')
    inputStream.setFilePath(path + '/test.xml')
    let stat = fileio.statSync(path + '/test.xml');
    let encoder: Encoder = new Encoder();
    if (!encoder.SetAlgorithm(2))
    return
    if (!encoder.SetDictionarySize(1 << 23))
    return
    if (!encoder.SetNumFastBytes(128))
    return
    if (!encoder.SetMatchFinder(1))
    return
    if (!encoder.SetLcLpPb(3, 0, 2))
    return
    encoder.SetEndMarkerMode(false);
    encoder.WriteCoderProperties(outputStream);
    let fileSize: Long = Long.fromNumber(stat.size);
    for (let i = 0; i < 8; i++) {
      outputStream.write(fileSize.shiftRightUnsigned(8 * i).toInt() & 0xFF);
    }
    encoder.Code(inputStream, outputStream, Long.fromNumber(-1), Long.fromNumber(-1), null);
    outputStream.flush();
    outputStream.close();
    inputStream.close();
    console.info(this.TAG + '结束')
  }

lzma,sevenz7 解压功能

指定文件夹路径解压lzma,sevenz7文件夹。

import {Decoder, Encoder,InputStream,OutputStream,Exception,System } from '@ohos/commons-compress'
import fileio from '@ohos.fileio';

decoder(path) {
    let decoder: Decoder = new Decoder();
    let inputStream: InputStream = new InputStream();
    inputStream.setFilePath(path + '/test.xml.lzma')
    let outputStream: OutputStream = new OutputStream();
    outputStream.setFilePath(path + '/test.xml')
    let propertiesSize = 5;
    let properties: Int8Array = new Int8Array(propertiesSize);
    if (inputStream.readBytesOffset(properties, 0, propertiesSize) != propertiesSize)
    throw new Exception("input .lzma file is too short");
    if (!decoder.SetDecoderProperties(properties))
    throw new Exception("Incorrect stream properties");
    let outSize: Long = Long.fromNumber(0);
    for (let i = 0; i < 8; i++) {
      let v: number = inputStream.read();
      if (v < 0)
      throw new Exception("Can't read stream size");
      outSize = outSize.or(Long.fromNumber(v).shiftLeft(8 * i));
    }
    if (!decoder.Code(inputStream, outputStream, outSize))
    throw new Exception("Error in data stream");
    outputStream.flush();
    outputStream.close();
    inputStream.close();
  }

tar 压缩功能

指定文件夹路径解压tar文件夹。

import { File, InputStream, OutputStream, ArchiveStreamFactory, TarArchiveEntry, IOUtils } from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  jsTarTest(): void {
    try{
        this.testArArchiveCreation(filesDir);
    }catch(error){
    }
  }

testArArchiveCreation(data: string) {
    try {
      let output: File = new File(data, this.newFolder + '.tar');
      let file1: File = new File(data + '/' + this.newFolder, 'test1.xml');
      let input1: InputStream = new InputStream();
      input1.setFilePath(file1.getPath());
      let out: OutputStream = new OutputStream();
      out.setFilePath(output.getPath());
      let os: ArchiveOutputStream = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream ("tar", out);
      let entry: TarArchiveEntry = new TarArchiveEntry();
      entry.tarArchiveEntryPreserveAbsolutePath2("testdata/test1.xml", false);
      entry.setModTime(Long.fromNumber(0));
      entry.setSize(Long.fromNumber(file1.length()));
      entry.setUserId(0);
      entry.setGroupId(0);
      entry.setUserName("avalon");
      entry.setGroupName("excalibur");
      entry.setMode(0o100000);
      os.putArchiveEntry(entry);
      IOUtils.copyStream(input1, os);
      os.closeArchiveEntry();
      os.close();
      AlertDialog.show({ title: '压缩成功',
        message: '请查看沙箱路径 ' + data + '/',
        confirm: { value: 'OK', action: () => {
          this.isDeCompressTarShow = true
        } }
      })
    } catch (e) {
      console.error("testArArchiveCreation " + e);
    }
  }

tar 解压功能

指定文件夹路径解压tar文件夹。

import { File, InputStream, OutputStream, TarArchiveInputStream, TarConstants, TarArchiveEntry } from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  jsUnTarTest(): void {
    try{
        this.testUnCompressTar(filesDir);
      }catch(error){
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }
testUnCompressTar(data: string) {
    let input: File = new File(data, this.newFolder + '.tar');
    let input1: InputStream = new InputStream();
    input1.setFilePath(input.getPath());
    let tais: TarArchiveInputStream = new TarArchiveInputStream(input1, TarConstants.DEFAULT_BLKSIZE,
      TarConstants.DEFAULT_RCDSIZE, null, false);
    let tarArchiveEntry: TarArchiveEntry = null;
    while ((tarArchiveEntry = tais.getNextTarEntry()) != null) {
      let name: string = tarArchiveEntry.getName();
      let tarFile: File = new File(data + '/' + this.newFolder, name);
      if (name.indexOf('/') != -1) {
        try {
          let splitName: string = name.substring(0, name.lastIndexOf('/'));
          fileio.mkdirSync(data + '/' + this.newFolder + '/' + splitName);
        } catch (err) {
        }
      }
      let fos: OutputStream = null;
      try {
        fos = new OutputStream();
        fos.setFilePath(tarFile.getPath())
        let read: number = -1;
        let buffer: Int8Array = new Int8Array(1024);
        while ((read = tais.readBytes(buffer)) != -1) {
          fos.writeBytesOffset(buffer, 0, read);
        }
        AlertDialog.show({ title: '解压成功',
          message: '请查看沙箱路径 ' + data + '/' + this.newFolder,
          confirm: { value: 'OK', action: () => {
            this.isDeCompressTarShow = true
          } }
        })
      } catch (e) {
        throw e;
      } finally {
        fos.close();
      }
    }
  }

snappy 压缩解压功能

指定文件夹路径压缩解压sz文件夹。

import { snappyCompress } from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  @State newfile: string = 'bla.txt'
  @State newfile1: string = 'bla1.txt'

  snappyJsTest(value) {
    try {
      var data = filesDir
      if (value) {
        let path = data + '/' + this.newfile
        console.log('snappyCompress');
        snappyCompress(path, path + '.sz')
          .then(() => {
            AlertDialog.show({ title: '压缩成功',
              message: '请查看沙箱路径 ' + path + '.sz',
              confirm: { value: 'OK', action: () => {
                this.isDeCompressSnappyShow = true
              } }
            })
          });
      } else {
        snappyUncompress(data + '/' + this.newfile + '.sz', data + '/' + this.newfile1)
          .then(() => {
            AlertDialog.show({ title: '解缩成功',
              message: '请查看沙箱路径 ' + data + '/' + this.newfile1,
              confirm: { value: 'OK', action: () => {
              } }
            })
          });
      }
    } catch (error) {
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

dump 解压功能

指定文件夹路径解压dump文件夹。

import { File, InputStream, OutputStream, ArchiveStreamFactory, ArchiveInputStream, ArchiveEntry, IOUtils } from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
jsDumpTest(): void {
     this.testDumpUnarchiveAll(filesDir,'dump/bla.dump')
}

testDumpUnarchiveAll(data: string, archive: string): void {
    let file1: File = new File(data, archive);
    //    fileio.mkdirSync(data+'/lost+found')
    let input1: InputStream = new InputStream();
    input1.setFilePath(file1.getPath());
    let input2: ArchiveInputStream = null;

    input2 = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("dump", input1, null);

    let entry: ArchiveEntry = input2.getNextEntry();
    while (entry != null) {
      let out: OutputStream = new OutputStream();
      let name: string = entry.getName().toString();
      let archiveEntry: File = new File(data, name);
      archiveEntry.getParentFile().getPath();

      if (entry.isDirectory()) {
        let splitName: string = name.substring(0, name.lastIndexOf('/'));
        try {
          fileio.mkdirSync(data + '/' + splitName);
        } catch (e) {
          console.log(e);
        }
        entry = input2.getNextEntry();
        continue;
      }
      let output: File = new File(data, name);
      out.setFilePath(output.getPath());
      IOUtils.copyStream(input2, out);
      out.close();
      out = null;
      entry = input2.getNextEntry();
    }
    if (input2 != null) {
      input2.close();
    }
    input1.close();
  }

deflate 压缩功能

指定文件夹路径压缩deflate文件夹。

import { DeflateFile } from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  DeflateFileTest(): void {
    try{
      var data = filesDir
        console.info('directory obtained. Data:' + data);
        DeflateFile(data + '/hello.txt', data + '/hello.txt.deflate')
          .then((isSuccess) => {
            if (isSuccess) {
              AlertDialog.show({ title: '压缩成功',
                message: '请查看沙箱路径 ' + data + '/test.txt.deflate',
                confirm: { value: 'OK', action: () => {
                  this.isDeCompressGZipShow = true
                } }
              })
            }
          });
    }catch(error){
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

deflate 解压功能

指定文件夹路径解压deflate文件夹。

import { InflateFile } from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
InflateFileTest(): void {
    try{
      var data = filesDir
        InflateFile(data + "/hello.txt.deflate", data + '/test.txt')
          .then(() => {
            AlertDialog.show({ title: '解缩成功',
              message: '请查看沙箱路径 ' + data + '/test.txt',
              confirm: { value: 'OK', action: () => {
              } }
            })
          });
    }catch(error){
      console.error('File to obtain the file directory. Cause: ' + error.message);
    }
  }

cpio 压缩功能

指定文件夹路径压缩cpio文件夹。

import {File, InputStream, OutputStream, ArchiveStreamFactory, ArchiveOutputStream, CpioArchiveEntry, IOUtils } from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");
  jsCpioTest(): void {
    try{
        this.testReadLongNamesBSD(filesDir)
      }catch(error){
        console.error('File to obtain the file directory. Cause: ' + error.message);
      }
  }

  testReadLongNamesBSD(data: string): void {
    let output: File = new File(data, this.newFolder + ".cpio");

    let file1: File = new File(data + '/' + this.newFolder, "test1.xml");
    let inputStream1 = new InputStream();
    inputStream1.setFilePath(file1.getPath());

    let out: OutputStream = new OutputStream();
    out.setFilePath(output.getPath());
    let os: ArchiveOutputStream = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream("cpio", out);
    let archiveEntry1: CpioArchiveEntry = new CpioArchiveEntry();
    archiveEntry1.initCpioArchiveEntryNameSize("test1.xml", Long.fromNumber(file1.length()));
    os.putArchiveEntry(archiveEntry1);
    IOUtils.copyStream(inputStream1, os);
    os.closeArchiveEntry();

    os.close();
    out.close();

    AlertDialog.show({ title: '压缩成功',
      message: '请查看沙箱路径 ' + data + '/',
      confirm: { value: 'OK', action: () => {
        this.isDeCompressGArShow = true
      } }
    })
  }

cpio 解压功能

指定文件夹路径解压cpio文件夹。

import {File, InputStream, OutputStream, CpioArchiveInputStream, CpioArchiveEntry, CpioConstants } from '@ohos/commons-compress'
import fileio from '@ohos.fileio';
let filesDir = GlobalContext.getContext().getObject("FilesDir");

jsUnCpioTest(): void {
try{
        this.testUnCompressCpio(filesDir)
      }catch(error){
        console.error('File to obtain the file directory. Cause: ' + error.message);
      }
}

  testUnCompressCpio(data: string) {
    let input: File = new File(data, this.newFolder + '.cpio');
    let input1: InputStream = new InputStream();
    input1.setFilePath(input.getPath());
    let tais = new CpioArchiveInputStream(input1, CpioConstants.BLOCK_SIZE, CharacterSetECI.ASCII.getName());
    let cpioArchiveEntry: CpioArchiveEntry = null;
    while ((cpioArchiveEntry = tais.getNextCPIOEntry()) != null) {
      let name: string = cpioArchiveEntry.getName();
      let tarFile: File = new File(data + '/' + this.newFolder, name);

      if (name.indexOf('/') != -1) {
        try {
          let splitName: string = name.substring(0, name.lastIndexOf('/'));
          fileio.mkdirSync(data + '/' + this.newFolder + '/' + splitName);
        } catch (err) {
        }
      }

      let fos: OutputStream = null;
      try {
        fos = new OutputStream();
        fos.setFilePath(tarFile.getPath())
        let read: number = -1;
        let buffer: Int8Array = new Int8Array(1024);
        while ((read = tais.readBytes(buffer)) != -1) {
          fos.writeBytesOffset(buffer, 0, read);
        }
      } catch (e) {
        throw e;
      } finally {
        fos.close();
      }
    }
    AlertDialog.show({ title: '解压成功',
      message: '请查看沙箱路径' + data + '/' + this.newFolder + '/test1.xml',
      confirm: { value: 'OK', action: () => {
        this.isCompressGArFileShow = true
      } }
    })
  }

目录

/commons-compress # 三方库源代码
├── src      # 框架代码
│   └── main
│   	└── cpp 
│           ├── zstd   	# # zstd C源码目录
│           └── zstd.cpp     # zstd Napi封装接口
│   	└── ets
│   		└── components
│       		└── archivers
│           		├── ar  	# ar源代码存放目录
│           		├── cpio   	# cpio源代码存放目录
│           		├── dump  	# dump源代码存放目录
│           		├── lzma    # lzma源代码存放目录
│           		├── tar   	# tar源代码存放目录
│           		└── zip     # zip源代码存放目录
│       		└── compressors
│           		├── brotli  # brotli源代码存放目录
│           		├── bzip2   # bzip2源代码存放目录
│           		├── lz77support  # lz77support源代码存放目录
│           		├── lzw     # lzw源代码存放目录
│           		├── snappy  # snappy源代码存放目录
│           		├── xz     	# xz源代码存放目录
│           		└── z    	# z源代码存放目录
│       		├── deflate     # deflate源代码存放目录
│       		├── gzip  # gzip源代码存放目录
│       		├── lz4   # lz4源代码存放目录
│       		├── util  # 工具源代码存放目录
│       		└── zstd  # zstd源代码存放目录

接口说明

接口 参数 功能
createArchiveOutputStream(archiverName: string, out: OutputStream) archiverName:存档名称
out:存档输出流
创建存档输出流。
zlib.compressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback): void inFile:要压缩的文件的路径。
inFile:要压缩的文件的路径。
Options:压缩文件的选项。
callback-压缩文件结果的回调。
zip压缩方法。
zlib.decompressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback): void inFile:要解压缩的文件的路径。
outFile:输出解压缩文件的路径。
Options:解压缩文件的选项。
callback:解压缩文件结果的回调。
zip解压方法。
gzipFile(src: string, dest: string) src:文件路径
dest:生成后的文件名称
gzip压缩方法。
unGzipFile(src: string, target: string) path:解压后的文件路径和名称
target:解压后的路径
gzip解压方法。
createCompressorOutputStream(name: string, out: OutputStream) name:压缩器名称
out:输出流
从存档程序名称和输出流创建存档输出流。
createCompressorInputStream( name: string, inputStream: InputStream, actualDecompressConcatenated: boolean) name:压缩器名称
inputStream:输入流
actualDecompressConcatenated:解压级
从存档程序名称和输入流创建存档输入流。
copy(input: InputStream, output: OutputStream) input:输入流
output:输出流
将输入流的内容复制到输出流中。
setFilePath(path: string) path:指定路径 打开指定文件路径。
createCompressorInputStream2(name: string, inputStream: InputStream) name:压缩器名称
inputStream:输入流
从压缩器名称和输入创建压缩器输入流。
readFully(input: InputStream, array: Int8Array) input:输入流
array:需要填充数组
从输入中读取尽可能多的信息,以填充给定的数组。
ZSTDCompress(path: string, filepath: string, level: number) path:需要压缩的文件包路径, filepath:压缩文件存放的路径,level:压缩等级(1~9) zstd压缩方法
ZSTDDecompress(path: string, dest: string) path:需要解压的文件包路径,dest:解压文件存放的路径 zstd解压方法。
lz4Compressed(src: string, dest: string) src:文件路径
dest:生成后的文件名称
压缩为lz4文件。
lz4Decompressed(src: string, target: string) path:解压后的文件路径和名称
target:解压后的路径
解压lz4文件。
createArchiveOutputStreamLittle(archiverName: string, out: OutputStream) archiverName:存档名称
out:输出流
创建存档输出流。
createArchiveInputStream( archiverName: string, inputStream: InputStream, actualEncoding: string) archiverName:存档名称
inputStream:输入流
actualEncoding:条目编码
从存档程序名称和输入流创建存档输入流。
snappyCompress(path, newfile) path:文件路径
newfile:生成后的文件名称
压缩为sz文件。
snappyUncompress(path, newfolder, newfile, newfile1) path:文件路径
newfolder: 文件名称
newfile:生成后的文件名称
newfile1:文件名称
解压sz文件。
DeflateFile(src: string, dest: string) src:文件路径
dest:生成后的文件名称
压缩为deflate文件。
InflateFile(src: string, target: string) src:解压后的文件路径和名称
target:解压后的路径
解压deflate文件。

约束与限制

在下述版本验证通过:

  • Deveco Studio:4.0 (4.0.3.512),SDK:API10 (4.0.10.9)

  • DevEco Studio版本: 4.0Canary1(4.0.3.212), SDK: API10(4.0.8.3)

开源协议

本项目基于 Apache License 2.0 ,请自由地享受和参与开源。

贡献代码

使用过程中发现任何问题都可以提 Issue 给我们,当然,我们也非常欢迎你给我们发 PR

相关项目

  • zstd —— Zstandard是一种快速无损压缩算法, 针对 ZLIB 级别的实时压缩场景和更好的压缩比。
  • 7z —— 7z是新的存档格式,提供高压缩比。
  • long.js —— long.js是一个 Long 类,用于表示从闭包库派生的 64 位二进制补码整数值,以供独立使用,并使用无符号支持进行扩展。
  • xz —— xz是一款免费的通用数据压缩软件,具有较高的压缩比。
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} 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.

简介

暂无描述 展开 收起
Apache-2.0
取消

发行版 (5)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/openharmony-tpc/CommonsCompress.git
git@gitee.com:openharmony-tpc/CommonsCompress.git
openharmony-tpc
CommonsCompress
CommonsCompress
master

搜索帮助