/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext.internal.misc;

public class MyZipFile implements AutoCloseable {

    public java.util.ArrayList<MyZipEntry> entries = new java.util.ArrayList<MyZipEntry>();

    public MyZipFile(String fileName, byte[] content) throws java.io.FileNotFoundException, java.io.IOException {
        if (content != null) 
            m_Stream = new com.pullenti.unisharp.MemoryStream(content);
        else 
            m_Stream = new com.pullenti.unisharp.FileStream(fileName, "r", "");
        m_Buf = new byte[4];
        while (true) {
            int i = m_Stream.read(m_Buf, 0, 4);
            if ((i != 4 || m_Buf[0] != ((byte)0x50) || m_Buf[1] != ((byte)0x4B)) || m_Buf[2] != ((byte)3) || m_Buf[3] != ((byte)4)) 
                break;
            MyZipEntry e = new MyZipEntry(this);
            this._readShort();
            int flags = this._readShort();
            if (((flags & 1)) != 0) 
                e.encrypted = true;
            e.method = this._readShort();
            this._readShort();
            this._readShort();
            int crc = this._readInt();
            e.compressDataSize = this._readInt();
            e.uncompressDataSize = this._readInt();
            int flen = this._readShort();
            int extr = this._readShort();
            byte[] fnam = new byte[flen];
            m_Stream.read(fnam, 0, flen);
            try {
                if (((flags & 0x800)) != 0) 
                    e.name = com.pullenti.util.MiscHelper.decodeStringUtf8(fnam, 0, -1);
                else {
                    for (i = 0; i < fnam.length; i++) {
                        int j = Byte.toUnsignedInt(fnam[i]);
                        if (j >= 0xE0 && j <= 0xEF) 
                            j -= 0x30;
                        if (j >= 0x80 && j <= 0xDF) 
                            fnam[i] = (byte)((j + 0x40));
                        if (j == 0xF1) 
                            fnam[i] = (byte)0xB8;
                        if (j == 0xF0) 
                            fnam[i] = (byte)0xA8;
                    }
                    e.name = com.pullenti.util.MiscHelper.decodeString1251(fnam, 0, -1);
                }
            } catch (Exception ex) {
                e.name = com.pullenti.util.MiscHelper.decodeString1251(fnam, 0, -1);
            }
            e.pos = ((int)m_Stream.getPosition()) + extr;
            if (extr > 10 && (e.compressDataSize < 0)) {
                int ii = this._readShort();
                if (ii == 1) {
                    this._readShort();
                    e.uncompressDataSize = this._readInt();
                    this._readInt();
                    e.compressDataSize = this._readInt();
                    this._readInt();
                }
            }
            m_Stream.setPosition((long)e.pos);
            if (e.compressDataSize > 0) 
                m_Stream.setPosition(m_Stream.getPosition() + ((long)e.compressDataSize));
            if (((flags & 8)) != 0 && e.compressDataSize == 0) {
                byte[] buf = new byte[10000];
                while (m_Stream.getPosition() < m_Stream.length()) {
                    int p0 = (int)m_Stream.getPosition();
                    i = m_Stream.read(buf, 0, buf.length);
                    if (i < 6) 
                        break;
                    for (int j = 0; j < (i - 4); j++) {
                        if (buf[j] == ((byte)0x50) && buf[j + 1] == ((byte)0x4B)) {
                            if (((buf[j + 2] == ((byte)3) && buf[j + 3] == ((byte)4))) || ((buf[j + 2] == ((byte)1) && buf[j + 3] == ((byte)2))) || ((buf[j + 2] == ((byte)5) && buf[j + 3] == ((byte)6)))) {
                                p0 += j;
                                e.compressDataSize = p0 - 12 - e.pos;
                                m_Stream.setPosition((long)(p0 - 4));
                                e.uncompressDataSize = this._readInt();
                                break;
                            }
                        }
                    }
                    if (e.compressDataSize > 0) 
                        break;
                    m_Stream.setPosition(m_Stream.getPosition() - (4L));
                }
            }
            entries.add(e);
        }
    }

    private com.pullenti.unisharp.Stream m_Stream;

    private byte[] m_Buf;

    private int _readShort() throws java.io.IOException {
        int i = m_Stream.read(m_Buf, 0, 2);
        if (i != 2) 
            return -1;
        i = Byte.toUnsignedInt(m_Buf[1]);
        i <<= 8;
        i |= (Byte.toUnsignedInt(m_Buf[0]));
        return i;
    }

    private int _readInt() throws java.io.IOException {
        int i = m_Stream.read(m_Buf, 0, 4);
        if (i != 4) 
            return -1;
        i = Byte.toUnsignedInt(m_Buf[3]);
        i <<= 8;
        i |= (Byte.toUnsignedInt(m_Buf[2]));
        i <<= 8;
        i |= (Byte.toUnsignedInt(m_Buf[1]));
        i <<= 8;
        i |= (Byte.toUnsignedInt(m_Buf[0]));
        return i;
    }

    @Override
    public void close() {
        try {
            if (m_Stream != null) 
                m_Stream.close();
        } catch (Exception ex123) {
        }
    }

    public byte[] unzip(MyZipEntry e) throws java.io.IOException, Exception {
        if (e.compressDataSize == 0) 
            return null;
        if (e.method == 8 || e.method == 0) {
        }
        else 
            return null;
        byte[] buf = new byte[e.compressDataSize];
        m_Stream.setPosition((long)e.pos);
        m_Stream.read(buf, 0, buf.length);
        if (e.method == 0) 
            return buf;
        return com.pullenti.util.ArchiveHelper.decompressDeflate(buf);
    }
    public MyZipFile() {
    }
}
