如何以编程方式解压缩.tgz文件

时间:2018-07-31 09:28:54

标签: java android

我正在尝试以编程方式解压缩.tgz,我正在使用junrar依赖来解压缩,但我面临以下问题

问题:

  

程序类型已经存在:   org.apache.commons.logging.LogConfigurationException   消息{种类=错误,文本=程序类型已存在:   org.apache.commons.logging.LogConfigurationException,来源= [未知   源文件],工具名称= Optional.of(D8)}

Dependancy :  implementation group: 'com.github.junrar', name: 'junrar', version: '0.7'

代码:解压缩.tgz文件

 private void extractAchieve(File archive, File destination) {

    Log.d("MainActivity", "Checking in extractArchieve");

    Archive arch = null;
    try {
        arch = new Archive(archive);
    } catch (RarException e) {
        e.printStackTrace();
        Log.d("MainActivity", "Checking while Archieve " + "Rar exception");
    } catch (IOException e) {
        Log.d("MainActivity", "Checking while Archieve " + "IO exception");
        e.printStackTrace();
    }

    if (arch != null) {
        if (arch.isEncrypted()) {
            Log.d("MainActivity", "Checking for UnRar " + "File encrypted");
        }

        FileHeader fh = null;

        while (true) {
            fh = arch.nextFileHeader();
            if (fh == null) {
                break;
            }

            if (fh.isEncrypted()) {
                Log.d("MainActivity", "Checking for UnRar " + "File encrypted " + fh.getFileNameString());
            }

            Log.d("MainActivity", "Checking Extracting " + fh.getFileNameString());

            if (fh.isDirectory()) {
                Log.d("MainActivity", "Checking Extracting " + "If file header is directory");
                //If extracted one is a directory
            } else {
                File file = createFile(fh, destination);
                try {
                    OutputStream outputStream = new FileOutputStream(file);
                    arch.extractFile(fh, outputStream);
                    outputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Log.d("MainActivity", "Checking Extracting " + "File not found exception");
                } catch (RarException e) {
                    e.printStackTrace();
                    Log.d("MainActivity", "Checking Extracting " + "Rar exception");
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.d("MainActivity", "Checking Extracting " + "IO exception");
                }
            }

        }

    }

}

private static File createFile(FileHeader fh, File destination) {
    Log.d("MainActivity", "Checking in createFile");
    File f = null;
    String name = null;
    if (fh.isFileHeader() && fh.isUnicode()) {
        name = fh.getFileNameW();
    } else {
        name = fh.getFileNameString();
    }
    f = new File(destination, name);
    if (!f.exists()) {
        try {
            f = makeFile(destination, name);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return f;
}

private static File makeFile(File destination, String name)
        throws IOException {
    Log.d("MainActivity", "Checking in makeFile");
    String[] dirs = name.split("\\\\");
    if (dirs == null) {
        return null;
    }
    String path = "";
    int size = dirs.length;
    if (size == 1) {
        return new File(destination, name);
    } else if (size > 1) {
        for (int i = 0; i < dirs.length - 1; i++) {
            path = path + File.separator + dirs[i];
            new File(destination, path).mkdir();
        }
        path = path + File.separator + dirs[dirs.length - 1];
        File f = new File(destination, path);
        f.createNewFile();
        return f;
    } else {
        return null;
    }
}

我尝试了以下情况:

  1. targetSdkVersion从26更改为27
  2. 将应用兼容版本形式从26.1.1更改为27.1.1

仍然没有用。

下面是我的gradle依赖项

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation group: 'com.github.junrar', name: 'junrar', version: '0.7'

如果有任何要取消安装的新实现,请告诉我。

1 个答案:

答案 0 :(得分:0)

JUnrar用于提取rar和zip文件。 GZip(.tgz)是完全不同的压缩。 您可以使用Chilkat库。请从下面的链接下载Chilkat,然后将其插入该页面上提到的Android项目中:http://www.chilkatsoft.com/chilkatAndroid.asp

将Chilkat库导入项目提取的.tgz文件,如下所示:

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    CkGzip gzip = new CkGzip();

    boolean success;

    //  Any string unlocks the component for the 1st 30-days.
    success = gzip.UnlockComponent("Anything for 30-day trial");
    if (success != true) {
        Log.i(TAG, gzip.lastErrorText());
        return;
    }

    //  Ungzip and untar.
    boolean bNoAbsolute = true;
    String untarToDirectory;
    untarToDirectory = "/temp/test";
    //  bNoAbsolute tells the component to convert all absolute paths
    //  found in the .tar to relative paths.   For example, if the .tar
    //  contains a file with an absolute path such as
    //  "/usr/bin/something.exe" it will
    //  be extracted to "/temp/test/usr/bin/something.exe";
    success = gzip.UnTarGz("test.tar.gz",untarToDirectory,bNoAbsolute);
    if (success != true) {
        Log.i(TAG, gzip.lastErrorText());
        return;
    }


  }

  static {
      // Important: Make sure the name passed to loadLibrary matches the shared library
      // found in your project's libs/armeabi directory.
      //  for "libchilkat.so", pass "chilkat" to loadLibrary
      //  for "libchilkatemail.so", pass "chilkatemail" to loadLibrary
      //  etc.
      // 
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

请注意,我还没有检查,但是我确定它会起作用。