Example #1
0
        // bool addDirectoryEntry
        public static void Test_OpenXml_Zip_02(string docxFile, string directory, bool useSlash)
        {
            //
            if (zFile.Exists(docxFile))
            {
                zFile.Delete(docxFile);
            }
            int l = directory.Length;

            if (!directory.EndsWith("\\"))
            {
                l++;
            }
            //using (FileStream fs = new FileStream(docxFile, FileMode.OpenOrCreate))
            using (FileStream fs = zFile.Open(docxFile, FileMode.OpenOrCreate))
                // Encoding.UTF8
                using (PBZipArchive zipArchive = new PBZipArchive(fs, ZipArchiveMode.Update, Encoding.UTF8))
                {
                    int fileCount      = 0;
                    int directoryCount = 0;
                    foreach (FileSystemInfo file in new DirectoryInfo(directory).EnumerateFileSystemInfos("*.*", SearchOption.AllDirectories))
                    {
                        string entryName = file.FullName.Substring(l);
                        if (useSlash)
                        {
                            entryName = entryName.Replace('\\', '/');
                        }
                        //if ((file.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
                        //{
                        //    if (useSlash)
                        //        entryName = entryName + "/";
                        //    else
                        //        entryName = entryName + "\\";
                        //    if (addDirectoryEntry)
                        //    {
                        //        Trace.WriteLine($"add directory \"{entryName}\"");
                        //        ZipArchiveEntry entry = zipArchive.CreateEntry(entryName);
                        //        directoryCount++;
                        //    }
                        //}
                        //else
                        if ((file.Attributes & FileAttributes.Directory) != FileAttributes.Directory)
                        {
                            Trace.WriteLine($"add file      \"{entryName}\"");
                            zipArchive.AddFile(file.FullName, entryName);
                            fileCount++;
                        }
                    }
                    Trace.WriteLine($"total {fileCount + directoryCount} entries {fileCount} files {directoryCount} directories");
                }
        }
Example #2
0
 // bool addDirectoryEntry
 public static void Test_OpenXml_Zip_02(string docxFile, string directory, bool useSlash)
 {
     //
     if (zFile.Exists(docxFile))
         zFile.Delete(docxFile);
     int l = directory.Length;
     if (!directory.EndsWith("\\"))
         l++;
     using (FileStream fs = new FileStream(docxFile, FileMode.OpenOrCreate))
     // Encoding.UTF8
     using (PBZipArchive zipArchive = new PBZipArchive(fs, ZipArchiveMode.Update, Encoding.UTF8))
     {
         int fileCount = 0;
         int directoryCount = 0;
         foreach (FileSystemInfo file in new DirectoryInfo(directory).EnumerateFileSystemInfos("*.*", SearchOption.AllDirectories))
         {
             string entryName = file.FullName.Substring(l);
             if (useSlash)
                 entryName = entryName.Replace('\\', '/');
             //if ((file.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
             //{
             //    if (useSlash)
             //        entryName = entryName + "/";
             //    else
             //        entryName = entryName + "\\";
             //    if (addDirectoryEntry)
             //    {
             //        Trace.WriteLine($"add directory \"{entryName}\"");
             //        ZipArchiveEntry entry = zipArchive.CreateEntry(entryName);
             //        directoryCount++;
             //    }
             //}
             //else
             if ((file.Attributes & FileAttributes.Directory) != FileAttributes.Directory)
             {
                 Trace.WriteLine($"add file      \"{entryName}\"");
                 zipArchive.AddFile(file.FullName, entryName);
                 fileCount++;
             }
         }
         Trace.WriteLine($"total {fileCount + directoryCount} entries {fileCount} files {directoryCount} directories");
     }
 }
Example #3
0
 public override bool IsCompressFile(string file)
 {
     return(ZipArchive.IsCompressFile(file));
 }
Example #4
0
 public override IEnumerable <string> Uncompress(string compressFile, string directory, IEnumerable <CompressFile> selectedFiles, UncompressOptions uncompressOptions = UncompressOptions.None)
 {
     return(ZipArchive.Unzip(compressFile, directory, selectedFiles, uncompressOptions));
 }
Example #5
0
        // use ZipArchive from System.IO.Compression
        //public override string[] Uncompress(string file, string directory, IEnumerable<string> selectedFiles = null, UncompressBaseOptions options = UncompressBaseOptions.None)
        //{
        //    bool extractFullPath = (options & UncompressBaseOptions.ExtractFullPath) == UncompressBaseOptions.ExtractFullPath;
        //    bool overrideExistingFile = (options & UncompressBaseOptions.OverrideExistingFile) == UncompressBaseOptions.OverrideExistingFile;
        //    bool renameExistingFile = (options & UncompressBaseOptions.RenameExistingFile) == UncompressBaseOptions.RenameExistingFile;
        //    List<string> files = new List<string>();

        //    Dictionary<string, string> dicCompressedFiles = null;
        //    if (selectedFiles != null)
        //    {
        //        dicCompressedFiles = new Dictionary<string, string>();
        //        foreach (string compressedFile in selectedFiles)
        //            dicCompressedFiles.Add(compressedFile, compressedFile);
        //    }

        //    using (System.IO.Compression.ZipArchive archive = ZipFile.OpenRead(file))
        //    {
        //        foreach (ZipArchiveEntry entry in archive.Entries)
        //        {
        //            if (dicCompressedFiles != null && !dicCompressedFiles.ContainsKey(entry.FullName))
        //                continue;

        //            string path = zPath.Combine(directory, entry.FullName);
        //            zfile.CreateFileDirectory(path);
        //            entry.ExtractToFile(path);
        //            files.Add(path);
        //        }
        //    }

        //    return files.ToArray();
        //}

        public override void Compress(string compressFile, IEnumerable <CompressFile> files, FileMode fileMode = FileMode.Create)
        {
            ZipArchive.Zip(compressFile, files, fileMode);
        }
Example #6
0
 private void ZipSourceFiles(ZipArchive zipArchive, IEnumerable<CompilerFile> files)
 {
     foreach (CompilerFile file in files)
     {
         if (zFile.Exists(file.File))
             zipArchive.AddFile(file.File, file.RelativePath);
         else
             WriteLine(1, "Compiler warning can't copy source file : source file \"{0}\" does not exist from project \"{1}\"", file.File, file.Project.ProjectFile);
     }
 }
Example #7
0
 private void CopySourceFiles()
 {
     // FileMode.Create will raz existing zip file
     if (_outputAssembly == null)
         throw new PBException("can't zip source files, output assembly is not defined");
     //_zipSourceFile = _zipSourceFilename;
     //if (_projectCompilerFile != null)
     //{
     //    string projectFile = zPath.GetFileNameWithoutExtension(_projectCompilerFile.File);
     //    if (projectFile.EndsWith(".project"))
     //        projectFile = projectFile.Substring(0, projectFile.Length - 8);
     //    _zipSourceFile = projectFile + "." + _zipSourceFile;
     //}
     //_zipSourceFile = _zipSourceFile.zRootPath(zPath.GetDirectoryName(_outputAssembly));
     _zipSourceFile = GetZipSourceFile(_outputAssembly);
     WriteLine(1, "  create zip source file \"{0}\"", _zipSourceFile);
     //using (ZipArchive zipArchive = new ZipArchive(_zipSourceFile, FileMode.Create))
     using (FileStream fs = new FileStream(_zipSourceFile, FileMode.Create))
     using (ZipArchive zipArchive = new ZipArchive(fs, System.IO.Compression.ZipArchiveMode.Update))
     {
         if (_projectCompilerFile != null && zFile.Exists(_projectCompilerFile.File))
             zipArchive.AddFile(_projectCompilerFile.File, _projectCompilerFile.RelativePath);
         if (_win32ResourceFile != null && zFile.Exists(_win32ResourceFile.File))
             zipArchive.AddFile(_win32ResourceFile.File, _win32ResourceFile.RelativePath);
         ZipSourceFiles(zipArchive, _sourceList.Values);
         ZipSourceFiles(zipArchive, _fileList.Values);
         ZipSourceFiles(zipArchive, _sourceFileList.Values);
     }
 }