Example #1
0
        public void ConvertSdp2Sdz(SpringPaths paths, Hash sdpHash, string targetSdz, Action <double> progressIndicator = null)
        {
            var sdpPath = Path.Combine(paths.WritableDirectory, "packages", sdpHash + ".sdp");
            var tempSdz = Path.Combine(paths.WritableDirectory, "temp", sdpHash + ".sdz");

            SdpArchive fileList;

            using (var fs = new FileStream(sdpPath, FileMode.Open)) fileList = new SdpArchive(new System.IO.Compression.GZipStream(fs, System.IO.Compression.CompressionMode.Decompress));

            GenerateSdz(paths, fileList, tempSdz, progressIndicator);

            tempSdz.RenameWithOverwrite(targetSdz);
            sdpPath.RenameWithOverwrite(Path.ChangeExtension(sdpPath, "sdpzk")); // rename sdp -> sdpzk
        }
            private static MemoryStream ReadFileFromSdp(SpringPaths paths, string namePath, string sdpPath)
            {
                if (File.Exists(sdpPath))
                {
                    SdpArchive sdp;
                    using (var fs = new FileStream(sdpPath, FileMode.Open)) sdp = new SdpArchive(new GZipStream(fs, CompressionMode.Decompress));

                    var entry = sdp.Files.FirstOrDefault(x => x.Name.ToLower() == namePath.Replace('\\', '/').ToLower());
                    if (entry != null)
                    {
                        return(new Pool(paths).ReadFromStorageDecompressed(entry.Hash));
                    }
                }
                return(null);
            }
Example #3
0
        /// <summary>
        /// Generates sdz archive from pool and file list
        /// </summary>
        private void GenerateSdz(SpringPaths paths, SdpArchive fileList, string tempSdz, Action <double> progressIndicator)
        {
            var pool = new Pool(paths);

            var dir = Path.GetDirectoryName(tempSdz);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            if (File.Exists(tempSdz))
            {
                File.Delete(tempSdz);
            }

            long uncompressedTotalSize = fileList.Files.Sum(x => x.UncompressedSize);
            long uncompressedProgress  = 0;


            using (var fs = new FileStream(tempSdz, FileMode.Create, FileAccess.Write, FileShare.None))
                using (var zip = new ZipOutputStream(fs, false))
                {
                    zip.CompressionLevel         = CompressionLevel.BestSpeed;
                    zip.ParallelDeflateThreshold = 0;
                    foreach (var fl in fileList.Files)
                    {
                        zip.PutNextEntry(fl.Name);

                        // 0 means paralell deflate is used, -1 means it is disabled
                        if (fl.UncompressedSize > paralellZipForFilesAboveSize)
                        {
                            zip.ParallelDeflateThreshold = 0;
                        }
                        else
                        {
                            zip.ParallelDeflateThreshold = -1;
                        }

                        using (var itemStream = pool.ReadFromStorageDecompressed(fl.Hash)) itemStream.CopyTo(zip);

                        uncompressedProgress += fl.UncompressedSize;

                        progressIndicator?.Invoke((double)uncompressedProgress / uncompressedTotalSize);
                    }
                }
        }
Example #4
0
            public MemoryStream ReadFile(SpringPaths paths, string namePath)
            {
                var sdpPath = Path.Combine(paths.WritableDirectory, "packages", $"{Hash}.sdp");

                if (File.Exists(sdpPath))
                {
                    SdpArchive sdp;
                    using (var fs = new FileStream(sdpPath, FileMode.Open)) sdp = new SdpArchive(new GZipStream(fs, CompressionMode.Decompress));

                    var entry = sdp.Files.FirstOrDefault(x => x.Name.ToLower() == namePath.Replace('\\', '/').ToLower());
                    if (entry != null)
                    {
                        return(new Pool(paths).ReadFromStorageDecompressed(entry.Hash));
                    }
                }
                return(null);
            }
Example #5
0
        SdpArchive GetFileList()
        {
            SdpArchive fileList;

            tempFilelist = GetTempFileName();
            fileListWebGet.Start(urlRoot + "/packages/" + PackageHash + ".sdp");
            fileListWebGet.WaitHandle.WaitOne();

            if (fileListWebGet.Result != null)
            {
                File.WriteAllBytes(tempFilelist, fileListWebGet.Result);
                using (var fs = new FileStream(tempFilelist, FileMode.Open)) fileList = new SdpArchive(new GZipStream(fs, CompressionMode.Decompress));
            }
            else
            {
                throw new ApplicationException("FileList has download failed");
            }
            return(fileList);
        }
Example #6
0
        bool LoadFiles(SdpArchive fileList)
        {
            try
            {
                doneAll = 0;

                var hashesToDownload = new List <Hash>();
                var bitArray         = new BitArray(fileList.Files.Count);
                for (var i = 0; i < fileList.Files.Count; i++)
                {
                    if (!pool.Exists(fileList.Files[i].Hash))
                    {
                        bitArray.PushBit(true);
                        hashesToDownload.Add(fileList.Files[i].Hash);
                    }
                    else
                    {
                        bitArray.PushBit(false);
                    }
                }

                var wr = WebRequest.Create(string.Format("{0}/streamer.cgi?{1}", urlRoot, PackageHash));
                wr.Method = "POST";
                wr.Proxy  = null;
                var zippedArray   = bitArray.GetByteArray().Compress();
                var requestStream = wr.GetRequestStream();
                requestStream.Write(zippedArray, 0, zippedArray.Length);
                requestStream.Close();

                var response = wr.GetResponse();
                Length = (int)(response.ContentLength + fileListWebGet.Length);
                var responseStream = response.GetResponseStream();

                var numberBuffer = new byte[4];
                var cnt          = 0;
                while (responseStream.ReadExactly(numberBuffer, 0, 4))
                {
                    if (IsAborted)
                    {
                        break;
                    }
                    var sizeLength = (int)SdpArchive.ParseUint32(numberBuffer);
                    var buf        = new byte[sizeLength];

                    if (!responseStream.ReadExactly(buf, 0, sizeLength, ref doneAll))
                    {
                        Trace.TraceError("{0} download failed - unexpected endo fo stream", Name);
                        return(false);
                    }
                    pool.PutToStorage(buf, hashesToDownload[cnt]);
                    cnt++;
                }
                if (cnt != hashesToDownload.Count)
                {
                    Trace.TraceError("{0} download failed - unexpected endo fo stream", Name);
                    return(false);
                }
                Trace.TraceInformation("{0} download complete - {1}", Name, Utils.PrintByteLength(Length));
                return(true);
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error downloading {0}: {1}", Name, ex);
                return(false);
            }
        }