/// <summary>
 /// Initializes a new instance of the <see cref="FileSystemProvider" /> class with the given base path.
 /// </summary>
 /// <param name="rootPath">The root path of this provider.</param>
 /// <param name="localBasePath">The path to a local directory where this instance will load the files from.</param>
 public ZipFileSystemProvider(string rootPath, string zipFilePath) : base(rootPath)
 {
     zipFile = new ZipFile(zipFilePath);
     zipFileEntries = zipFile.GetAllEntries()
         .Where(x => x.FilenameInZip.StartsWith("assets/data/"))
         .ToDictionary(x => x.FilenameInZip
             .Replace("assets/data/", string.Empty),
             x => x);
 }
        /// <summary>
        /// Add all the entries from a zip file on the system.
        /// </summary>
        /// <param name="path">
        /// Path to the zip file
        /// </param>
        public void MergeZipFile(string path)
        {
            ZipFileEntry[] merge;
            using (var zip = new ZipFile(path))
            {
                merge = zip.GetAllEntries();
            }

            this.AddZipFileEntries(merge);
        }
示例#3
0
        private static void CopyFileOrDirectory(Logger logger, string sourceDir, string root, string path)
        {
            // Root path not handled
            if (root.Length == 0)
                throw new NotSupportedException();

            try
            {
                var zipFile = new ZipFile(sourceDir);
                foreach (var zipEntry in zipFile.GetAllEntries())
                {
                    var zipFilename = zipEntry.FilenameInZip;
                    if (!zipFilename.StartsWith(root))
                        continue;

                    // Get filename without leading "assets/data"
                    var assetFilename = zipFilename.Substring(root.Length);

                    var fullPath = path + assetFilename;
                    var directoryName = VirtualFileSystem.GetParentFolder(fullPath);

                    try
                    {
                        if (directoryName != string.Empty)
                            ApplicationData.CreateDirectory(directoryName);
                    }
                    catch (IOException)
                    {
                    }

                    using (var output = ApplicationData.OpenStream(fullPath, VirtualFileMode.Create, VirtualFileAccess.Write))
                        zipFile.ExtractFile(zipEntry, output);
                }
            }
            catch (IOException ex)
            {
                logger.Info("I/O Exception", ex);
            }
        }
        /// <summary>
        /// Ensures that the zip file is valid.
        /// </summary>
        /// <param name="validationHandler">
        /// The class to hold the object for progress reporting
        /// </param>
        /// <returns>
        /// True if the zip is valid, otherwise False.
        /// </returns>
        public static bool Validate(ZipFileValidationHandler validationHandler)
        {
            var buf = new byte[1024 * 256];

            try
            {
                var            zip     = new ZipFile(validationHandler.Filename);
                ZipFileEntry[] entries = zip.GetAllEntries();

                // First calculate the total compressed length
                long  totalCompressedLength = entries.Sum(entry => entry.CompressedSize);
                float averageVerifySpeed    = 0;
                long  totalBytesRemaining   = totalCompressedLength;
                validationHandler.TotalBytes = totalCompressedLength;

                // Then calculate a CRC for every file in the Zip file,
                // comparing it to what is stored in the Zip directory
                foreach (ZipFileEntry entry in entries)
                {
                    if (entry.Crc32 != -1)
                    {
                        DateTime startTime = DateTime.UtcNow;

                        var crc = new Crc32();

                        uint offset = entry.FileOffset;
                        var  length = (int)entry.CompressedSize;

                        Stream raf = zip.zipFileStream;
                        raf.Seek(offset, SeekOrigin.Begin);

                        while (length > 0)
                        {
                            int seek = length > buf.Length ? buf.Length : length;
                            raf.Read(buf, 0, seek);
                            crc.Update(buf, 0, seek);
                            length -= seek;

                            DateTime currentTime = DateTime.UtcNow;
                            var      timePassed  = (float)(currentTime - startTime).TotalMilliseconds;
                            if (timePassed > 0)
                            {
                                float currentSpeedSample = seek / timePassed;
                                if (averageVerifySpeed <= 0)
                                {
                                    averageVerifySpeed = (SmoothingFactor * currentSpeedSample)
                                                         + ((1 - SmoothingFactor) * averageVerifySpeed);
                                }
                                else
                                {
                                    averageVerifySpeed = currentSpeedSample;
                                }

                                totalBytesRemaining -= seek;
                                var timeRemaining = (long)(totalBytesRemaining / averageVerifySpeed);

                                validationHandler.AverageSpeed  = averageVerifySpeed;
                                validationHandler.CurrentBytes  = totalCompressedLength - totalBytesRemaining;
                                validationHandler.TimeRemaining = timeRemaining;

                                validationHandler.UpdateUi(validationHandler);
                            }

                            startTime = currentTime;
                            if (validationHandler.ShouldCancel)
                            {
                                return(true);
                            }
                        }

                        if (crc.Value != entry.Crc32)
                        {
                            Debug.WriteLine("CRC does not match for entry: " + entry.FilenameInZip);
                            Debug.WriteLine("In file: " + entry.ZipFileName);

                            return(false);
                        }
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
                return(false);
            }

            return(true);
        }
示例#5
0
        /// <summary>
        /// Add all the entries from a zip file on the system.
        /// </summary>
        /// <param name="path">
        /// Path to the zip file
        /// </param>
        public void MergeZipFile(string path)
        {
            ZipFileEntry[] merge;
            using (var zip = new ZipFile(path))
            {
                merge = zip.GetAllEntries();
            }

            this.AddZipFileEntries(merge);
        }
        /// <summary>
        /// Ensures that the zip file is valid.
        /// </summary>
        /// <param name="validationHandler">
        /// The class to hold the object for progress reporting
        /// </param>
        /// <returns>
        /// True if the zip is valid, otherwise False.
        /// </returns>
        public static bool Validate(ZipFileValidationHandler validationHandler)
        {
            var buf = new byte[1024 * 256];
            try
            {
                var zip = new ZipFile(validationHandler.Filename);
                ZipFileEntry[] entries = zip.GetAllEntries();

                // First calculate the total compressed length
                long totalCompressedLength = entries.Sum(entry => entry.CompressedSize);
                float averageVerifySpeed = 0;
                long totalBytesRemaining = totalCompressedLength;
                validationHandler.TotalBytes = totalCompressedLength;

                // Then calculate a CRC for every file in the Zip file,
                // comparing it to what is stored in the Zip directory
                foreach (ZipFileEntry entry in entries)
                {
                    if (entry.Crc32 != -1)
                    {
                        DateTime startTime = DateTime.UtcNow;

                        var crc = new Crc32();

                        uint offset = entry.FileOffset;
                        var length = (int)entry.CompressedSize;

                        Stream raf = zip.zipFileStream;
                        raf.Seek(offset, SeekOrigin.Begin);

                        while (length > 0)
                        {
                            int seek = length > buf.Length ? buf.Length : length;
                            raf.Read(buf, 0, seek);
                            crc.Update(buf, 0, seek);
                            length -= seek;

                            DateTime currentTime = DateTime.UtcNow;
                            var timePassed = (float)(currentTime - startTime).TotalMilliseconds;
                            if (timePassed > 0)
                            {
                                float currentSpeedSample = seek / timePassed;
                                if (averageVerifySpeed <= 0)
                                {
                                    averageVerifySpeed = (SmoothingFactor * currentSpeedSample)
                                                         + ((1 - SmoothingFactor) * averageVerifySpeed);
                                }
                                else
                                {
                                    averageVerifySpeed = currentSpeedSample;
                                }

                                totalBytesRemaining -= seek;
                                var timeRemaining = (long)(totalBytesRemaining / averageVerifySpeed);

                                validationHandler.AverageSpeed = averageVerifySpeed;
                                validationHandler.CurrentBytes = totalCompressedLength - totalBytesRemaining;
                                validationHandler.TimeRemaining = timeRemaining;

                                validationHandler.UpdateUi(validationHandler);
                            }

                            startTime = currentTime;
                            if (validationHandler.ShouldCancel)
                            {
                                return true;
                            }
                        }

                        if (crc.Value != entry.Crc32)
                        {
                            Debug.WriteLine("CRC does not match for entry: " + entry.FilenameInZip);
                            Debug.WriteLine("In file: " + entry.ZipFileName);

                            return false;
                        }
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
                return false;
            }

            return true;
        }