コード例 #1
0
        public static ZipArchiveEntry CreateEntryFromFolder(this ZipArchive destination, string sourceFolderName, string entryName, CompressionLevel compressionLevel)
        {
            string sourceFolderFullPath = Path.GetFullPath(sourceFolderName);
            string basePath = entryName + "/";

            var createdFolders = new HashSet<string>();

            var entry = destination.CreateEntry(basePath);
            createdFolders.Add(basePath);

            foreach (string dirFolder in Directory.EnumerateDirectories(sourceFolderName, "*.*", SearchOption.AllDirectories))
            {
                string dirFileFullPath = Path.GetFullPath(dirFolder);
                string relativePath = (basePath + dirFileFullPath.Replace(sourceFolderFullPath + Path.DirectorySeparatorChar, ""))
                    .Replace(Path.DirectorySeparatorChar, '/');
                string relativePathSlash = relativePath + "/";

                if (!createdFolders.Contains(relativePathSlash))
                {
                    destination.CreateEntry(relativePathSlash, compressionLevel);
                    createdFolders.Add(relativePathSlash);
                }
            }

            foreach (string dirFile in Directory.EnumerateFiles(sourceFolderName, "*.*", SearchOption.AllDirectories))
            {
                string dirFileFullPath = Path.GetFullPath(dirFile);
                string relativePath = (basePath + dirFileFullPath.Replace(sourceFolderFullPath + Path.DirectorySeparatorChar, ""))
                    .Replace(Path.DirectorySeparatorChar, '/');
                destination.CreateEntryFromFile(dirFile, relativePath, compressionLevel);
            }

            return entry;
        }
コード例 #2
0
		public static void AddDirectory(this ZipArchive archive, string sourceDir)
		{
			DirectoryInfo directoryInfo = new DirectoryInfo(sourceDir);
			sourceDir = directoryInfo.FullName;

			foreach (FileSystemInfo entry in directoryInfo.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
			{
				string relativePath = entry.FullName.Substring(sourceDir.Length, entry.FullName.Length - sourceDir.Length);
				relativePath = relativePath.TrimStart(new char[]
				{
					Path.DirectorySeparatorChar,
					Path.AltDirectorySeparatorChar
				});

				if (entry is FileInfo)
				{
					archive.AddFile(entry.FullName, relativePath);
				}
				else
				{
					DirectoryInfo subDirInfo = entry as DirectoryInfo;
					if (subDirInfo != null && !subDirInfo.EnumerateFileSystemInfos().Any())
					{
						archive.CreateEntry(relativePath + Path.DirectorySeparatorChar);
					}
				}
			}
		}
コード例 #3
0
ファイル: ZipFileExtensions.cs プロジェクト: nlhepler/mono
		public static ZipArchiveEntry CreateEntryFromFile (
			this ZipArchive destination, string sourceFileName,
			string entryName, CompressionLevel compressionLevel)
		{
			if (destination == null)
				throw new ArgumentNullException ("destination");

			if (sourceFileName == null)
				throw new ArgumentNullException ("sourceFileName");

			if (entryName == null)
				throw new ArgumentNullException ("entryName");

			ZipArchiveEntry entry;
			using (Stream stream = File.Open (sourceFileName, FileMode.Open,
				FileAccess.Read, FileShare.Read))
			{
				var zipArchiveEntry = destination.CreateEntry (entryName, compressionLevel);

				using (Stream entryStream = zipArchiveEntry.Open ())
					stream.CopyTo (entryStream);

				entry = zipArchiveEntry;
			}

			return entry;
		}
コード例 #4
0
ファイル: ZipArchiveExtensions.cs プロジェクト: haefele/Savvy
        public static ZipArchiveEntry GetOrCreateNewEntry(this ZipArchive archive, string path)
        {
            var entry = archive.GetOrCreateEntry(path);
            entry.Delete();

            return archive.CreateEntry(path);
        }
コード例 #5
0
ファイル: ZipExtensions.cs プロジェクト: eerhardt/NuGet3
        public static void AddEntry(this ZipArchive archive, string path, byte[] data)
        {
            var entry = archive.CreateEntry(path);

            using (var stream = entry.Open())
            {
                stream.Write(data, 0, data.Length);
            }
        }
コード例 #6
0
ファイル: ZipArchiveExtensions.cs プロジェクト: haefele/Savvy
        public static ZipArchiveEntry GetOrCreateEntry(this ZipArchive archive, string path)
        {
            var existingEntry = archive.Entries.FirstOrDefault(f => string.Equals(f.FullName.NormalizePath(), path.NormalizePath(), StringComparison.OrdinalIgnoreCase));

            if (existingEntry != null)
                return existingEntry;

            return archive.CreateEntry(path);
        }
コード例 #7
0
 public static void AddFile(this ZipArchive zipArchive, FileInfoBase file, string directoryNameInArchive)
 {
     string fileName = Path.Combine(directoryNameInArchive, file.Name);
     ZipArchiveEntry entry = zipArchive.CreateEntry(fileName, CompressionLevel.Fastest);
     using (Stream zipStream = entry.Open(),
                   fileStream = file.OpenRead())
     {
         fileStream.CopyTo(zipStream);
     }
 }
コード例 #8
0
ファイル: ZipExtensions.cs プロジェクト: eerhardt/NuGet3
        public static void AddEntry(this ZipArchive archive, string path, string value, Encoding encoding)
        {
            var entry = archive.CreateEntry(path);

            using (var stream = entry.Open())
            {
                var data = encoding.GetBytes(value);
                stream.Write(data, 0, data.Length);
            }
        }
コード例 #9
0
		/// <summary>
		/// Extension method for Rename an Entry in ZIP Archive.
		/// </summary>
		/// <param name="archive">ZipArchive</param>
		/// <param name="oldName">Current entry name to rename</param>
		/// <param name="newName">New name</param>
		public static void RenameEntry(this ZipArchive archive, string oldName, string newName)
		{
			ZipArchiveEntry oldEntry = archive.GetEntry(oldName),
				newEntry = archive.CreateEntry(newName);

			using (Stream oldStream = oldEntry.Open())
			using (Stream newStream = newEntry.Open())
			{
				oldStream.CopyTo(newStream);
			}

			oldEntry.Delete();
		}
コード例 #10
0
		private static ZipArchiveEntry AddFile(this ZipArchive archive, string sourceFile, string targetRelativePath)
		{
			ZipArchiveEntry entry;
			using (Stream sourceStream = File.OpenRead(sourceFile))
			{
				entry = archive.CreateEntry(targetRelativePath);
				using (Stream targetStream = entry.Open())
				{
					sourceStream.CopyTo(targetStream);
				}
			}
			return entry;
		}
コード例 #11
0
        public static void RenameEntry(this ZipArchive archive, string oldName, string newName)
        {
            ZipArchiveEntry oldEntry = archive.GetEntry(oldName),
                newEntry = archive.CreateEntry(newName);

            using (Stream oldStream = oldEntry.Open())
            using (Stream newStream = newEntry.Open())
            {
                oldStream.CopyTo(newStream);
            }

            oldEntry.Delete();

               //TestRepoChangeCommit2

            /******    ZipArchive archive = ...; open archive in "update" mode
                       string oldName = ...,
                       newName = ...; // names initialized as appropriate

                  archive.RenameEntry(oldName, newName); ******/
        }
コード例 #12
0
        public static void AddDirectory(this ZipArchive zipArchive, DirectoryInfoBase directory, string directoryNameInArchive)
        {
            bool any = false;
            foreach (var info in directory.GetFileSystemInfos())
            {
                any = true;
                var subDirectoryInfo = info as DirectoryInfoBase;
                if (subDirectoryInfo != null)
                {
                    string childName = Path.Combine(directoryNameInArchive, subDirectoryInfo.Name);
                    zipArchive.AddDirectory(subDirectoryInfo, childName);
                }
                else
                {
                    zipArchive.AddFile((FileInfoBase)info, directoryNameInArchive);
                }
            }

            if (!any)
            {
                // If the directory did not have any files or folders, add a entry for it
                zipArchive.CreateEntry(EnsureTrailingSlash(directoryNameInArchive));
            }
        }
コード例 #13
0
 public static void AddFile(this ZipArchive zip, string fileName, string fileContent)
 {
     ZipArchiveEntry entry = zip.CreateEntry(fileName, CompressionLevel.Fastest);
     using (var writer = new StreamWriter(entry.Open()))
     {
         writer.Write(fileContent);
     }
 }
コード例 #14
0
 static ZipArchiveEntry Entry(this ZipArchive archive, string filename)
 {
     return archive.GetEntry(filename) ?? archive.CreateEntry(filename);
 }
コード例 #15
-1
ファイル: ZipArchiveExtensions.cs プロジェクト: 40a/kudu
        public static void AddFile(this ZipArchive zipArchive, FileInfoBase file, ITracer tracer, string directoryNameInArchive)
        {
            Stream fileStream = null;
            try
            {
                fileStream = file.OpenRead();
            }
            catch (Exception ex)
            {
                // tolerate if file in use.
                // for simplicity, any exception.
                tracer.TraceError(String.Format("{0}, {1}", file.FullName, ex));
                return;
            }

            try
            {
                string fileName = ForwardSlashCombine(directoryNameInArchive, file.Name);
                ZipArchiveEntry entry = zipArchive.CreateEntry(fileName, CompressionLevel.Fastest);
                entry.LastWriteTime = file.LastWriteTime;

                using (Stream zipStream = entry.Open())
                {
                    fileStream.CopyTo(zipStream);
                }
            }
            finally
            {
                fileStream.Dispose();
            }
        }