【问题标题】:how to extract 7z file android studio如何提取7z文件android studio
【发布时间】:2017-04-08 14:28:11
【问题描述】:

我正在编写一个需要提取 7z 档案的 Android 应用程序。时间紧迫,我正在搜索可以在我的项目中使用的第三方库或源代码。

我将 .so 文件复制到 /libs 文件夹中

app\src\main\jniLibs\armeabi\libun7z.so

我无法在 android 中提取 7z 文件

AndUn7z.extract7z(My7z, My7zPath);

运行此行后出现此错误:

java.lang.UnsatisfiedLinkError: No implementation found for int com.darkgamers.pepsiman.browser.mainmenu.AndUn7z.un7zip(java.lang.String, java.lang.String) (试过 Java_com_darkgamers_pepsiman_browser_mainmenu_AndUn7z_un7zip 和 Java_com_darkgamers_pepsiman_browser_mainmenu_AndUn7z_un7zip__Ljava_lang 在 com.darkgamers.pepsiman.browser.mainmenu.AndUn7z.un7zip(Native Method)

这是我的代码:

public class AndUn7z {

public static boolean extract7z(String filePath, String outPath)
{
    File outDir = new File(outPath);
    if(!outDir.exists() || !outDir.isDirectory())
    {
        outDir.mkdirs();
    }
    return (AndUn7z.un7zip(filePath, outPath) == 1);
}

/**
 * Extract from assets
 * @param context
 * @param assetPath
 * @param outPath
 * @return
 * @throws Exception
 */
public static boolean extractAssets(Context context, String assetPath, String outPath)
{
    File outDir = new File(outPath);
    if(!outDir.exists() || !outDir.isDirectory())
    {
        outDir.mkdirs();
    }

    String tempPath = outPath + File.separator + ".temp";
    try {
        copyFromAssets(context, assetPath, tempPath);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    boolean ret = (AndUn7z.un7zip(tempPath, outPath) == 1);
    new File(tempPath).delete();

    return ret;
}

/**
 * Copy asset to temp
 * @param context
 * @param assetPath
 * @param tempPath
 * @throws Exception
 */
private static void copyFromAssets(Context context, String assetPath, String tempPath)
        throws Exception
{
    InputStream inputStream = context.getAssets().open(assetPath);
    FileOutputStream fileOutputStream = new FileOutputStream(tempPath);
    int length = -1;
    byte[] buffer = new byte[0x400000];
    while ((length = inputStream.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, length);
    }
    fileOutputStream.flush();
    fileOutputStream.close();
    inputStream.close();
}

//JNI interface
private static native int un7zip(String filePath, String outPath);

static {
    System.loadLibrary("un7z");
}

}

【问题讨论】:

  • 任何错误输出?
  • 问题已更新。
  • 请任何人帮助我 - 我无法处理这个

标签: android extract 7zip


【解决方案1】:

下载7z SDK for Java并使用它的LzmaAlone类,如下所示:

    BufferedInputStream inStream  = new BufferedInputStream(ourInputfile, BUFFER_SIZE);
    BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(ourOutputfile), BUFFER_SIZE);

    int propertiesSize = 5;
    byte[] properties = new byte[propertiesSize];

    if (inStream.read(properties, 0, propertiesSize) != propertiesSize)
        throw new IOException("input .lzma file is too short");

    Decoder decoder = new Decoder();

    if (!decoder.SetDecoderProperties(properties))
        throw new IllegalArgumentException("Incorrect stream properties");

    long outSize = 0;

    for (int i = 0; i < 8; i++)
    {
        int v = inStream.read();
        if (v < 0)
            throw new IOException("Can't read stream size");
        outSize |= ((long)v) << (8 * i);
    }

    if (!decoder.Code(inStream, outStream, outSize))
        throw new IOException("Error in data stream");

    outStream.flush();
    outStream.close();
    inStream.close();

【讨论】:

    【解决方案2】:

    在使用 7z 并知道您要求使用 7z 之后,xz 中的 Java API 变得更加容易:

    XZInputStream in = new XZInputStream(your_input_stream_xz_compressed);
    FileOutputStream out = new FileOutputStream(your_output_file_decompressed);
    byte[] buffer = new byte[1024];
    int length;
    
    while ((length = in.read(buffer)) != -1) // Copy DB byte for bite
      out.write(buffer, 0, length);
    
    out.flush();
    out.close();
    in.close();
    

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      相关资源
      最近更新 更多