Exemplo n.º 1
0
        public static bool IsZipFile(string fileName)
        {
            Requires.NotEmpty(fileName);

            using (var fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                // Load End Of Central Directory Record
                var zipEoCDR = new TZipEndOfCentralDirectoryRecord();
                zipEoCDR.Load(fileStream);

                if (zipEoCDR.Position > 0)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        public static bool IsZipFile(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            using (var fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                // Load End Of Central Directory Record
                var zipEoCDR = new TZipEndOfCentralDirectoryRecord();
                zipEoCDR.Load(fileStream);

                if (zipEoCDR.Position > 0)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads Zip file central directory for later access.
        /// </summary>
        /// <param name="containerPath">Path to main zip container (e.g. MyZip.zip).</param>
        /// <returns></returns>
        public bool Init(string containerPath)
        {
            if (string.IsNullOrWhiteSpace(containerPath))
            {
                throw new ArgumentNullException(nameof(containerPath));
            }
            Report.BeginTimed("Init Zip container '" + containerPath + "'.");

            FileStream mainFileStream = null;

            FileStream[] zipStreams = new FileStream[0];

            try
            {
                mainFileStream = File.Open(containerPath, FileMode.Open, FileAccess.Read, FileShare.None);

                // Load End Of Central Directory Record
                var zipEoCDR = new TZipEndOfCentralDirectoryRecord();
                zipEoCDR.Load(mainFileStream);

                if (zipEoCDR.Position < 0)
                {
                    throw new Exception("ZipFile: couldn't find central directory record.");
                }

                int  numberOfEntries = zipEoCDR.cd_totalEntries;
                long cd_offset       = zipEoCDR.cd_offset;
                int  cd_diskId       = zipEoCDR.cd_diskId;

                // init number of zip containers
                m_ZipFileNames = new string[zipEoCDR.cd_diskId + 1];
                m_ZipFileNames[zipEoCDR.cd_diskId] = containerPath;

                zipStreams = new FileStream[zipEoCDR.cd_diskId + 1];
                zipStreams[zipEoCDR.cd_diskId] = mainFileStream;

                // Load all parts of a multi zip
                if (zipEoCDR.cd_diskId > 0)
                {
                    Report.Line("Is multi zip container.");

                    // Segment 1 = filename.z01
                    // Segment n-1 = filename.z(n-1)
                    // Segment n = filename.zip

                    var baseName =
                        0 == containerPath.Substring(containerPath.Length - 4).ToLower().CompareTo(".zip") ?
                        containerPath.Substring(0, containerPath.Length - 4) :
                        containerPath;
                    baseName += ".z";

                    for (int i = 0; i < zipEoCDR.cd_diskId; i++)
                    {
                        m_ZipFileNames[i] = baseName + (i + 1).ToString("D2");
                        zipStreams[i]     = File.Open(m_ZipFileNames[i], FileMode.Open, FileAccess.Read, FileShare.None);
                    }
                }

                // Try to load Zip64 End Of Central Directory Locator
                var zip64Locator = new TZip64EndOfCentralDirectoryLocator();
                zip64Locator.Load(mainFileStream, zipEoCDR.Position);

                if (zip64Locator.IsValid)
                {
                    Report.Line("Is Zip64 container.");

                    // Load Zip64 End of Central Directory Record
                    var zip64EoCDR = new TZip64EndOfCentralDirectoryRecord();
                    zip64EoCDR.Load(
                        zipStreams[zip64Locator.ecd64_diskId],
                        (long)zip64Locator.ecd64_relOffset
                        );

                    numberOfEntries = (int)zip64EoCDR.cd_totalEntries;
                    cd_offset       = (long)zip64EoCDR.cd_offset;
                    cd_diskId       = (int)zip64EoCDR.cd_diskId;
                }

                // Load file headers in central directory
                this.ReadZipCentralDirectoryFileHeaders(zipStreams, cd_diskId, cd_offset, numberOfEntries);

                // save weak links to streams
                m_ZipFileStreams = new WeakReference[zipStreams.Length];
                for (int i = 0; i < zipStreams.Length; i++)
                {
                    m_ZipFileStreams[i] = new WeakReference(zipStreams[i]);
                }
            }
            catch (Exception)
            {
                Report.Warn("Error initializing container.");
                Report.End();

                foreach (var stream in zipStreams)
                {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }

                if (mainFileStream != null)
                {
                    mainFileStream.Close();
                }

                return(false);
            }

            Report.End("Init Zip container finished.");
            return(true);
        }