Esempio n. 1
0
            /// <summary>
            /// Given a path to a Zip file and a toc_entry, return the (uncompressed)
            /// data as a new reference.
            /// </summary>
            /// <param name="archive"></param>
            /// <param name="toc_entry"></param>
            /// <returns></returns>
            private byte[] GetData(CodeContext context, string archive, PythonTuple toc_entry)
            {
                int          l, file_offset = (int)toc_entry[4], data_size = (int)toc_entry[2], compress = (int)toc_entry[1];
                BinaryReader fp = null;

                byte[] raw_data;
                byte[] data = null;
                try {
                    try {
                        fp = new BinaryReader(new FileStream(archive, FileMode.Open, FileAccess.Read));
                    } catch {
                        throw PythonOps.IOError("zipimport: can not open file {0}", archive);
                    }

                    // Check to make sure the local file header is correct
                    fp.BaseStream.Seek(file_offset, SeekOrigin.Begin);
                    l = fp.ReadInt32();
                    if (l != 0x04034B50)
                    {
                        // Bad: Local File Header
                        throw MakeError(context, "bad local file header in {0}", archive);
                    }
                    fp.BaseStream.Seek(file_offset + 26, SeekOrigin.Begin);
                    l            = 30 + fp.ReadInt16() + fp.ReadInt16(); // local header size
                    file_offset += l;                                    // start of file data

                    fp.BaseStream.Seek(file_offset, SeekOrigin.Begin);
                    try {
                        raw_data = fp.ReadBytes(compress == 0 ? data_size : data_size + 1);
                    } catch {
                        throw PythonOps.IOError("zipimport: can't read data");
                    }

                    if (compress != 0)
                    {
                        raw_data[data_size] = (byte)'Z';
                        data_size++;
                    }

                    if (compress == 0)   // data is not compressed
                    {
                        data = raw_data;
                    }
                    else
                    {
                        // decompress with zlib
                        data = ZlibModule.Decompress(raw_data, -15);
                    }
                } finally {
                    if (fp != null)
                    {
                        ((IDisposable)fp).Dispose();
                    }
                }

                return(data);
            }
Esempio n. 2
0
            public bool GetData(PackedResourceInfo tocEntry, out byte[] result, out string unpackingError)
            {
                unpackingError = null;
                result         = null;
                var fileOffset = tocEntry.FileOffset;
                var dataSize   = tocEntry.DataSize;
                var compress   = tocEntry.Compress;

                try {
                    var stream = GetZipArchive();
                    if (stream == null)
                    {
                        unpackingError = "Resource not found.";
                        return(false);
                    }
                    using (var reader = new BinaryReader(stream)) {
                        // Check to make sure the local file header is correct
                        reader.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
                        var l = reader.ReadInt32();
                        if (l != 0x04034B50)
                        {
                            // Bad: Local File Header
                            unpackingError = "Bad local file header in ZIP resource.";
                            return(false);
                        }
                        reader.BaseStream.Seek(fileOffset + 26, SeekOrigin.Begin);
                        l           = 30 + reader.ReadInt16() + reader.ReadInt16(); // local header size
                        fileOffset += l;                                            // start of file data

                        reader.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
                        byte[] rawData;
                        try {
                            rawData = reader.ReadBytes(compress == 0 ? dataSize : dataSize + 1);
                        }
                        catch {
                            unpackingError = "Can't read data";
                            return(false);
                        }

                        if (compress != 0)
                        {
                            rawData[dataSize] = (byte)'Z';
                        }

                        result = compress == 0 ? rawData : ZlibModule.Decompress(rawData, -15);
                        return(true);
                    }
                }
                catch (Exception exception) {
                    unpackingError = String.Format("{0}: {1}", exception.GetType().Name, exception.Message);
                    return(false);
                }
            }
        public BinaryStream fopen(Env env, StringValue path, StringValue mode,
                                  LongValue options)
        {
            bool useIncludePath =
                (options.toLong() & StreamModule.STREAM_USE_PATH) != 0;

            Value pathComponent
                = UrlModule.parse_url(env, path, UrlModule.PHP_URL_PATH);

            if (!pathComponent.isset())
            {
                log.info(L.l("no path component found in '{0}'", path.ToString()));
                return(null);
            }

            return(ZlibModule.gzopen(env, pathComponent.ToStringValue(),
                                     mode.ToString(),
                                     useIncludePath));
        }