首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何提取7z文件

如何提取7z文件
EN

Stack Overflow用户
提问于 2016-11-24 10:22:14
回答 2查看 1.6K关注 0票数 0

我正在编写一个需要时间提取7z archives.Pressed的Android应用程序,我已经搜索了可以在我的项目中使用的第三方库或源代码。

我在.so文件夹中复制/libs文件

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

我不能在android中提取7z文件

AndUn7z.extract7z(My7z,My7zPath);

运行这一行后,会出现以下错误:

com.darkgamers.pepsiman.browser.mainmenu.AndUn7z.un7zip(java.lang.String,:没有为int java.lang.UnsatisfiedLinkError找到实现(尝试了Java_com_darkgamers_pepsiman_browser_mainmenu_AndUn7z_un7zip和Java_com_darkgamers_pepsiman_browser_mainmenu_AndUn7z_un7zip__Ljava_lang_String_2Ljava_lang_String_2) at com.darkgamers.pepsiman.browser.mainmenu.AndUn7z.un7zip(Native方法)

这是我的密码:

代码语言:javascript
复制
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");
}

}

EN

回答 2

Stack Overflow用户

发布于 2018-07-16 06:53:22

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

代码语言:javascript
复制
    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();
票数 1
EN

Stack Overflow用户

发布于 2018-08-04 08:58:55

在使用了7z并知道您要求7z之后,Java在xz中要容易得多。

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40783781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档