无法从 Android 上创建的 ZIP 存档中提取文件(特定于设备/操作系统) [英] Cannot extract file from ZIP archive created on Android (device/OS specific)

查看:28
本文介绍了无法从 Android 上创建的 ZIP 存档中提取文件(特定于设备/操作系统)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用如下代码在 Android 上创建存档:

I am creating an archive on Android using the code like this:

    OutputStream os = new FileOutputStream(zipFile);
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
    try 
    {
        zos.setLevel(8);
        byte[] buffer = new byte[32768];
        for (VFile src : toPack)            
        {
            ZipEntry entry = new ZipEntry(src.name);
            zos.putNextEntry(entry);
            src.pushToStream(zos, buffer);
            src.close();
            zos.closeEntry();
        }
    }
    finally 
    {
        zos.close();
    }       

我发现只有一种压缩方法可用 - DEFLATED(只有 STORED 替代方法可用).这意味着存档总是用一种方法压缩.

I found that there's only one compression method available - DEFLATED (there's only STORED alternative available). This means that archive is always compressed with one method.

如果我在 Android 2.3.4 上运行此代码 - 我可以使用 7Zip 在 Windows 中解压缩文件;如果我在 Android 3(或三星 Galaxy Tab;不确定是谁弄错了)上运行它 - 7Zip 显示存档列表,但无法解压缩文件,显示 不支持的压缩方法.同时 7Zip 将 Deflate 显示为一种文件压缩方法,这意味着它可以正确处理它.

If I run this code on Android 2.3.4 - I can decompress files in Windows using 7Zip; if I run this on Android 3 (or Samsung Galaxy Tab; not sure who makes it wrong) - 7Zip shows archive list, but cannot decompress file saying Unsupported compression method. At the same time 7Zip shows Deflate as a compression method on file which means it can treat it properly.

有人遇到过这个问题吗?

Did anyone has this problem?

谢谢.

UDP:发现另一个 有类似问题的主题(虽然可能不一样).

UDP: Found another topic with similar issue (may be not same though).

推荐答案

@user1269737 的回答是正确的;几乎.但它只适用于单文件档案.下面是解析整个存档的代码.

The @user1269737's answer is correct; almost. But it only works for a single-file archives. Below is a code which parses the whole archive.

/**
* Replace wrong local file header byte
* http://sourceforge.net/tracker/?func=detail&aid=3477810&group_id=14481&atid=114481
* Applies to Android API 9-13
* @param zip file
* @throws IOException
*/
private static void fixInvalidZipFile(File zip) throws IOException 
{
    RandomAccessFile r = new RandomAccessFile(zip, "rw");
    try
    {
        long eocd_offset = findEOCDRecord(r);

        if (eocd_offset > 0)
        {
            r.seek(eocd_offset + 16);  // offset of first CDE in EOCD               
            long cde_offset = readInt(r);  // read offset of first Central Directory Entry
            long lfh_offset = 0;
            long fskip, dskip;

            while (true)
            {
                r.seek(cde_offset);
                if (readInt(r) != CDE_SIGNATURE)  // got off sync!
                    return;

                r.seek(cde_offset + 20);  // compressed file size offset                
                fskip = readInt(r);

                // fix the header
                //
                r.seek(lfh_offset + 7);
                short localFlagsHi = r.readByte();  // hi-order byte of local header flags (general purpose)
                r.seek(cde_offset + 9);
                short realFlagsHi = r.readByte();  // hi-order byte of central directory flags (general purpose)
                if (localFlagsHi != realFlagsHi)
                { // in latest versions this bug is fixed, so we're checking is bug exists.
                    r.seek(lfh_offset + 7);
                    r.write(realFlagsHi);
                }

                //  calculate offset of next Central Directory Entry
                //
                r.seek(cde_offset + 28);  // offset of variable CDE parts length in CDE
                dskip = 46;  // length of fixed CDE part
                dskip += readShort(r);  // file name
                dskip += readShort(r);  // extra field
                dskip += readShort(r);  // file comment

                cde_offset += dskip;
                if (cde_offset >= eocd_offset)  // finished!
                    break;              

                // calculate offset of next Local File Header
                //
                r.seek(lfh_offset + 26);  // offset of variable LFH parts length in LFH
                fskip += readShort(r);  // file name
                fskip += readShort(r);  // extra field
                fskip += 30;  // length of fixed LFH part
                fskip += 16;  // length of Data Descriptor (written after file data)

                lfh_offset += fskip;
            }
        }
    }
    finally
    {
        r.close();
    }
}

//http://www.pkware.com/documents/casestudies/APPNOTE.TXT
private static final int LFH_SIGNATURE = 0x04034b50;
private static final int DD_SIGNATURE = 0x08074b50;
private static final int CDE_SIGNATURE = 0x02014b50;
private static final int EOCD_SIGNATURE = 0x06054b50;

/** Find an offset of End Of Central Directory record in file */
private static long findEOCDRecord(RandomAccessFile f) throws IOException
{
    long result = f.length() - 22; // 22 is minimal EOCD record length
    while (result > 0)
    {
        f.seek(result);

        if (readInt(f) == EOCD_SIGNATURE) return result;

        result--;
    }
    return -1;
}

/** Read a 4-byte integer from file converting endianness. */
private static int readInt(RandomAccessFile f) throws IOException
{
    int result = 0;
    result |= f.read();
    result |= (f.read() << 8);
    result |= (f.read() << 16);
    result |= (f.read() << 24);
    return result;
}

/** Read a 2-byte integer from file converting endianness. */
private static short readShort(RandomAccessFile f) throws IOException
{
    short result = 0;
    result |= f.read();
    result |= (f.read() << 8);
    return result;
}

这篇关于无法从 Android 上创建的 ZIP 存档中提取文件(特定于设备/操作系统)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆