Exemplo n.º 1
0
        public void Zip_Create_New()
        {
            foreach (var file in Directory.EnumerateFiles(ORIGINAL_FILES_PATH, "*.*", SearchOption.AllDirectories))
            {
                var newFileName = file.Substring(ORIGINAL_FILES_PATH.Length);
                if (newFileName.StartsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    newFileName = newFileName.Substring(1);
                }
                newFileName = Path.Combine(SCRATCH_FILES_PATH, newFileName);
                var newDir = Path.GetDirectoryName(newFileName);
                if (!Directory.Exists(newDir))
                {
                    Directory.CreateDirectory(newDir);
                }
                File.Copy(file, newFileName);
            }
            string scratchPath = Path.Combine(SCRATCH2_FILES_PATH, "Zip.deflate.noEmptyDirs.zip");
            string unmodified  = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip");

            using (var archive = ZipArchive.Create())
            {
                archive.AddAllFromDirectory(SCRATCH_FILES_PATH);

                WriterOptions writerOptions = new ZipWriterOptions(CompressionType.Deflate);
                writerOptions.ArchiveEncoding.Default = Encoding.GetEncoding(866);

                archive.SaveTo(scratchPath, writerOptions);
            }
            CompareArchivesByPath(unmodified, scratchPath);
            Directory.Delete(SCRATCH_FILES_PATH, true);
        }
Exemplo n.º 2
0
        public void CreateZipArchive(string filename, long files, long filesize, long chunksize, bool set_zip64, bool forward_only)
        {
            var data = new byte[chunksize];

            // Use deflate for speed
            var opts = new ZipWriterOptions(CompressionType.Deflate)
            {
                UseZip64 = set_zip64
            };

            // Use no compression to ensure we hit the limits (actually inflates a bit, but seems better than using method==Store)
            var eo = new ZipWriterEntryOptions()
            {
                DeflateCompressionLevel = Compressors.Deflate.CompressionLevel.None
            };

            using (var zip = File.OpenWrite(filename))
                using (var st = forward_only ? (Stream) new ForwardOnlyStream(zip) : zip)
                    using (var zipWriter = (ZipWriter)WriterFactory.Open(st, ArchiveType.Zip, opts))
                    {
                        for (var i = 0; i < files; i++)
                        {
                            using (var str = zipWriter.WriteToStream(i.ToString(), eo))
                            {
                                var left = filesize;
                                while (left > 0)
                                {
                                    var b = (int)Math.Min(left, data.Length);
                                    str.Write(data, 0, b);
                                    left -= b;
                                }
                            }
                        }
                    }
        }
Exemplo n.º 3
0
        public static void FileBackup(WebsiteInfo websiteInfo)
        {
            if (websiteInfo.UseBackup.ToLower() == "true")
            {
                string backupFileName;
                if (websiteInfo.BackupRate.ToLower() == "day")
                {
                    backupFileName = Path.Combine(websiteInfo.BackupFolder, websiteInfo.Name + DateTime.Now.ToString("_yyyyMMdd") + ".zip");
                }
                else if (websiteInfo.BackupRate.ToLower() == "hour")
                {
                    backupFileName = Path.Combine(websiteInfo.BackupFolder, websiteInfo.Name + DateTime.Now.ToString("_yyyyMMdd_HH") + ".zip");
                }
                else if (websiteInfo.BackupRate.ToLower() == "minute")
                {
                    backupFileName = Path.Combine(websiteInfo.BackupFolder, websiteInfo.Name + DateTime.Now.ToString("_yyyyMMdd_HHmm") + ".zip");
                }
                else
                {
                    Console.WriteLine("备份频率有误,不进行备份");
                    return;
                }

                if (File.Exists(backupFileName))
                {
                    Console.WriteLine("已备份,不进行备份");
                    return;
                }
                Console.WriteLine("开始备份...");
                Directory.CreateDirectory(Path.GetDirectoryName(backupFileName));

                var fs = File.Open(backupFileName, FileMode.CreateNew);
                var ci = new ZipWriterOptions(CompressionType.LZMA);
                ci.DeflateCompressionLevel = CompressionLevel.BestCompression;
                using (ZipWriter w = new ZipWriter(fs, ci)) {
                    var cDir = new DirectoryInfo(websiteInfo.WebsiteFolder);
                    ReadFolder(websiteInfo, cDir, w);
                }
                fs.Dispose();

                //using (ZipFile zip = new ZipFile(Encoding.Default)) {
                //    var cDir = new DirectoryInfo(websiteInfo.WebsiteFolder);
                //    ReadFolder(websiteInfo, cDir, zip);
                //    var fs = File.Open(backupFileName, FileMode.CreateNew);
                //    zip.Save(fs);
                //    fs.Dispose();
                //}
                Console.WriteLine("备份成功...");
            }
            else
            {
                Console.WriteLine("不进行备份");
                return;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Constructs a new zip instance.
        /// If the file exists and has a non-zero length we read it,
        /// otherwise we create a new archive.
        /// </summary>
        /// <param name="filename">The name of the file to read or write</param>
        /// <param name="options">The options passed on the commandline</param>
        public FileArchiveZip(string filename, Dictionary <string, string> options)
        {
            if (string.IsNullOrEmpty(filename) && filename.Trim().Length == 0)
            {
                throw new ArgumentException("filename");
            }

            if (File.Exists(filename) && new FileInfo(filename).Length > 0)
            {
                m_isWriting = false;
                m_filename  = filename;
            }
            else
            {
                var compression = new ZipWriterOptions(CompressionType.Deflate);

                compression.CompressionType         = DEFAULT_COMPRESSION_METHOD;
                compression.DeflateCompressionLevel = DEFAULT_COMPRESSION_LEVEL;

                m_usingZip64 = compression.UseZip64 =
                    options.ContainsKey(COMPRESSION_ZIP64_OPTION)
                    ? Duplicati.Library.Utility.Utility.ParseBoolOption(options, COMPRESSION_ZIP64_OPTION)
                    : DEFAULT_ZIP64;

                string          cpmethod;
                CompressionType tmptype;
                if (options.TryGetValue(COMPRESSION_METHOD_OPTION, out cpmethod) && Enum.TryParse <SharpCompress.Common.CompressionType>(cpmethod, true, out tmptype))
                {
                    compression.CompressionType = tmptype;
                }

                string cplvl;
                int    tmplvl;
                if (options.TryGetValue(COMPRESSION_LEVEL_OPTION, out cplvl) && int.TryParse(cplvl, out tmplvl))
                {
                    compression.DeflateCompressionLevel = (SharpCompress.Compressors.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0);
                }
                else if (options.TryGetValue(COMPRESSION_LEVEL_OPTION_ALIAS, out cplvl) && int.TryParse(cplvl, out tmplvl))
                {
                    compression.DeflateCompressionLevel = (SharpCompress.Compressors.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0);
                }

                m_defaultCompressionLevel = compression.DeflateCompressionLevel;
                m_compressionType         = compression.CompressionType;

                m_isWriting = true;
                m_stream    = new System.IO.FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read);
                m_writer    = WriterFactory.Open(m_stream, ArchiveType.Zip, compression);

                //Size of endheader, taken from SharpCompress ZipWriter
                m_flushBufferSize = 8 + 2 + 2 + 4 + 4 + 2 + 0;
            }
        }
Exemplo n.º 5
0
Arquivo: ZIP.cs Projeto: fileaes/FAES
        /// <summary>
        /// Compress (ZIP) an unencrypted FAES File.
        /// </summary>
        /// <param name="unencryptedFile">Unencrypted FAES File</param>
        /// <returns>Path of the unencrypted, ZIP compressed file</returns>
        public string CompressFAESFile(FAES_File unencryptedFile)
        {
            FileAES_IntUtilities.CreateEncryptionFilePath(unencryptedFile, "ZIP", out string tempRawPath, out _, out string tempOutputPath);

            ZipWriterOptions wo = new ZipWriterOptions(CompressionType.Deflate)
            {
                DeflateCompressionLevel = _compressLevel
            };

            using (Stream stream = File.OpenWrite(tempOutputPath))
                using (var writer = new ZipWriter(stream, wo))
                {
                    writer.WriteAll(tempRawPath, "*", SearchOption.AllDirectories);
                }
            return(tempOutputPath);
        }
Exemplo n.º 6
0
        public void Zip_Random_Write_Add()
        {
            string jpg         = Path.Combine(ORIGINAL_FILES_PATH, "jpg", "test.jpg");
            string scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Zip.deflate.mod.zip");
            string unmodified  = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod.zip");
            string modified    = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod2.zip");

            using (var archive = ZipArchive.Open(unmodified))
            {
                archive.AddEntry("jpg\\test.jpg", jpg);

                WriterOptions writerOptions = new ZipWriterOptions(CompressionType.Deflate);
                writerOptions.ArchiveEncoding.Default = Encoding.GetEncoding(866);

                archive.SaveTo(scratchPath, writerOptions);
            }
            CompareArchivesByPath(modified, scratchPath);
        }
Exemplo n.º 7
0
        public void Zip_Random_Write_Remove()
        {
            string scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Zip.deflate.mod.zip");
            string unmodified  = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip");
            string modified    = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod.zip");

            using (var archive = ZipArchive.Open(unmodified))
            {
                var entry = archive.Entries.Single(x => x.Key.EndsWith("jpg"));
                archive.RemoveEntry(entry);

                WriterOptions writerOptions = new ZipWriterOptions(CompressionType.Deflate);
                writerOptions.ArchiveEncoding.Default = Encoding.GetEncoding(866);

                archive.SaveTo(scratchPath, writerOptions);
            }
            CompareArchivesByPath(modified, scratchPath);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Constructs a new zip instance.
        /// Access mode is specified by mode parameter.
        /// Note that stream would not be disposed by FileArchiveZip instance so
        /// you may reuse it and have to dispose it yourself.
        /// </summary>
        /// <param name="stream">The stream to read or write depending access mode</param>
        /// <param name="mode">The archive acces mode</param>
        /// <param name="options">The options passed on the commandline</param>
        public FileArchiveZip(Stream stream, ArchiveMode mode, IDictionary <string, string> options)
        {
            m_stream = stream;
            m_mode   = mode;
            if (mode == ArchiveMode.Write)
            {
                var compression = new ZipWriterOptions(CompressionType.Deflate);

                compression.CompressionType         = DEFAULT_COMPRESSION_METHOD;
                compression.DeflateCompressionLevel = DEFAULT_COMPRESSION_LEVEL;

                m_usingZip64 = compression.UseZip64 =
                    options.ContainsKey(COMPRESSION_ZIP64_OPTION)
                    ? Duplicati.Library.Utility.Utility.ParseBoolOption(options, COMPRESSION_ZIP64_OPTION)
                    : DEFAULT_ZIP64;

                string          cpmethod;
                CompressionType tmptype;
                if (options.TryGetValue(COMPRESSION_METHOD_OPTION, out cpmethod) && Enum.TryParse <SharpCompress.Common.CompressionType>(cpmethod, true, out tmptype))
                {
                    compression.CompressionType = tmptype;
                }

                string cplvl;
                int    tmplvl;
                if (options.TryGetValue(COMPRESSION_LEVEL_OPTION, out cplvl) && int.TryParse(cplvl, out tmplvl))
                {
                    compression.DeflateCompressionLevel = (SharpCompress.Compressors.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0);
                }
                else if (options.TryGetValue(COMPRESSION_LEVEL_OPTION_ALIAS, out cplvl) && int.TryParse(cplvl, out tmplvl))
                {
                    compression.DeflateCompressionLevel = (SharpCompress.Compressors.Deflate.CompressionLevel)Math.Max(Math.Min(9, tmplvl), 0);
                }

                m_defaultCompressionLevel = compression.DeflateCompressionLevel;
                m_compressionType         = compression.CompressionType;

                m_writer = WriterFactory.Open(m_stream, ArchiveType.Zip, compression);

                //Size of endheader, taken from SharpCompress ZipWriter
                m_flushBufferSize = 8 + 2 + 2 + 4 + 4 + 2 + 0;
            }
        }
Exemplo n.º 9
0
        public override void RunJob(DistributedTask distributedTask, CancellationToken cancellationToken)
        {
            base.RunJob(distributedTask, cancellationToken);

            using var scope = ThirdPartyOperation.CreateScope();
            var scopeClass = scope.ServiceProvider.GetService <FileDownloadOperationScope>();

            var(globalStore, filesLinkUtility, _, _, _) = scopeClass;
            using var stream = TempStream.Create();

            var writerOptions = new ZipWriterOptions(CompressionType.Deflate);

            writerOptions.ArchiveEncoding.Default = Encoding.UTF8;
            writerOptions.DeflateCompressionLevel = SharpCompress.Compressors.Deflate.CompressionLevel.Level3;

            using (var zip = WriterFactory.Open(stream, ArchiveType.Zip, writerOptions))
            {
                (ThirdPartyOperation as FileDownloadOperation <string>).CompressToZip(zip, stream, scope);
                (DaoOperation as FileDownloadOperation <int>).CompressToZip(zip, stream, scope);
            }

            if (stream != null)
            {
                stream.Position = 0;
                const string fileName = FileConstant.DownloadTitle + ".zip";
                var          store    = globalStore.GetStore();
                store.Save(
                    FileConstant.StorageDomainTmp,
                    string.Format(@"{0}\{1}", ((IAccount)Thread.CurrentPrincipal.Identity).ID, fileName),
                    stream,
                    "application/zip",
                    "attachment; filename=\"" + fileName + "\"");
                Status = string.Format("{0}?{1}=bulk", filesLinkUtility.FileHandlerPath, FilesLinkUtility.Action);
            }

            FillDistributedTask();
            TaskInfo.PublishChanges();
        }