/// <summary> /// Добавляет директорию в указанную коллекцию кассеты. Возвращает добавленные в базу данных элементы /// </summary> /// <param name="dir"></param> /// <param name="collectionId"></param> /// <returns>Возвращает добавленные в базу данных элементы</returns> public static IEnumerable<XElement> AddDirectory(this Cassette cassette, DirectoryInfo dir, string collectionId) { List<XElement> addedElements = new List<XElement>(); var dname = dir.FullName; var shortname = dir.Name; // TODO: Надо учесть русские имена и с пробелами var newcoll_id = "collection_" + cassette.Name + "_" + dname; // Поищу, нет ли такой коллекции в базе данных var collection_found = cassette.db.Elements(ONames.TagCollection) .FirstOrDefault((XElement e) => { var xabout = e.Attribute(ONames.rdfabout); return (xabout != null && xabout.Value == newcoll_id); }); if(collection_found != null) { } else { // Создам коллекцию XElement coll = new XElement(ONames.TagCollection, new XAttribute(ONames.rdfabout, newcoll_id), new XElement(ONames.TagName, shortname)); cassette.db.Add(coll); addedElements.Add(coll); // Присоединим коллекцию к вышестоящей XElement collmem = new XElement(ONames.TagCollectionmember, new XAttribute(ONames.rdfabout, "cm_" + newcoll_id), new XElement(ONames.TagCollectionitem, new XAttribute(ONames.rdfresource, newcoll_id)), new XElement(ONames.TagIncollection, new XAttribute(ONames.rdfresource, collectionId))); cassette.db.Add(collmem); addedElements.Add(collmem); cassette.Changed = true; } // Обработаем файлы и директории foreach(var f in dir.GetFiles()) { if(f.Name == "Thumbs.db") { } else addedElements.AddRange(cassette.AddFile(f, newcoll_id)); } foreach(var d in dir.GetDirectories()) addedElements.AddRange(cassette.AddDirectory(d, newcoll_id)); return addedElements; }
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)); } }
public static void AddDirectory(this ZipArchive zipArchive, string directoryPath, string directoryNameInArchive = "") { var directoryInfo = new DirectoryInfoWrapper(new DirectoryInfo(directoryPath)); zipArchive.AddDirectory(directoryInfo, directoryNameInArchive); }
public static MockFileSystem WithDirectory(this MockFileSystem mockFileSystem, string path) { mockFileSystem.AddDirectory(mockFileSystem.Path.Combine(mockFileSystem.Directory.GetCurrentDirectory(), path)); return mockFileSystem; }
/// <summary> /// Adds an item, either a file or a directory, to a zip file archive, /// explicitly specifying the directory path to be used in the archive. /// </summary> /// /// <remarks> /// <para> /// If adding a directory, the add is recursive on all files and /// subdirectories contained within it. /// </para> /// <para> /// The name of the item may be a relative path or a fully-qualified path. /// The item added by this call to the <c>ZipFile</c> is not read from the /// disk nor written to the zip file archive until the application calls /// Save() on the <c>ZipFile</c>. /// </para> /// /// <para> /// This version of the method allows the caller to explicitly specify the /// directory path to be used in the archive, which would override the /// "natural" path of the filesystem file. /// </para> /// /// <para> /// Encryption will be used on the file data if the <c>Password</c> has /// been set on the <c>ZipFile</c> object, prior to calling this method. /// </para> /// /// <para> /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see /// cref="Password"/>, <see cref="SetCompression"/>, <see /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>, /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their /// respective values at the time of this call will be applied to the /// <c>ZipEntry</c> added. /// </para> /// /// </remarks> /// /// <exception cref="System.IO.FileNotFoundException"> /// Thrown if the file or directory passed in does not exist. /// </exception> /// /// <param name="fileOrDirectoryName">the name of the file or directory to add. /// </param> /// /// <param name="directoryPathInArchive"> /// The name of the directory path to use within the zip archive. This path /// need not refer to an extant directory in the current filesystem. If the /// files within the zip are later extracted, this is the path used for the /// extracted file. Passing <c>null</c> (<c>Nothing</c> in VB) will use the /// path on the fileOrDirectoryName. Passing the empty string ("") will /// insert the item at the root path within the archive. /// </param> /// /// <seealso cref="Ionic.Zip.ZipFile.AddFile(string, string)"/> /// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string, string)"/> /// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string, string)"/> /// /// <example> /// This example shows how to zip up a set of files into a flat hierarchy, /// regardless of where in the filesystem the files originated. The resulting /// zip archive will contain a toplevel directory named "flat", which itself /// will contain files Readme.txt, MyProposal.docx, and Image1.jpg. A /// subdirectory under "flat" called SupportFiles will contain all the files /// in the "c:\SupportFiles" directory on disk. /// /// <code> /// String[] itemnames= { /// "c:\\fixedContent\\Readme.txt", /// "MyProposal.docx", /// "c:\\SupportFiles", // a directory /// "images\\Image1.jpg" /// }; /// /// try /// { /// using (ZipFile zip = new ZipFile()) /// { /// for (int i = 1; i < itemnames.Length; i++) /// { /// // will add Files or Dirs, recurses and flattens subdirectories /// zip.AddItem(itemnames[i],"flat"); /// } /// zip.Save(ZipToCreate); /// } /// } /// catch (System.Exception ex1) /// { /// System.Console.Error.WriteLine("exception: {0}", ex1); /// } /// </code> /// /// <code lang="VB"> /// Dim itemnames As String() = _ /// New String() { "c:\fixedContent\Readme.txt", _ /// "MyProposal.docx", _ /// "SupportFiles", _ /// "images\Image1.jpg" } /// Try /// Using zip As New ZipFile /// Dim i As Integer /// For i = 1 To itemnames.Length - 1 /// ' will add Files or Dirs, recursing and flattening subdirectories. /// zip.AddItem(itemnames(i), "flat") /// Next i /// zip.Save(ZipToCreate) /// End Using /// Catch ex1 As Exception /// Console.Error.WriteLine("exception: {0}", ex1.ToString()) /// End Try /// </code> /// </example> /// <returns>The <c>ZipEntry</c> added.</returns> public static ZipEntry AddItem(this ZipFile zipFile, String fileOrDirectoryName, String directoryPathInArchive) { string fullPath = GetFullPath(fileOrDirectoryName); if (FileSystem.Current.GetFileFromPathAsync(fullPath).ExecuteSync() != null) return zipFile.AddFile(fileOrDirectoryName, directoryPathInArchive); if (FileSystem.Current.GetFolderFromPathAsync(fullPath).ExecuteSync() != null) return zipFile.AddDirectory(fileOrDirectoryName, directoryPathInArchive); throw new FileNotFoundException(String.Format("That file or directory ({0}) does not exist!", fileOrDirectoryName)); }
/// <summary> /// Adds the contents of a filesystem directory to a Zip file archive. /// </summary> /// /// <remarks> /// /// <para> /// The name of the directory may be a relative path or a fully-qualified /// path. Any files within the named directory are added to the archive. Any /// subdirectories within the named directory are also added to the archive, /// recursively. /// </para> /// /// <para> /// Top-level entries in the named directory will appear as top-level entries /// in the zip archive. Entries in subdirectories in the named directory will /// result in entries in subdirectories in the zip archive. /// </para> /// /// <para> /// If you want the entries to appear in a containing directory in the zip /// archive itself, then you should call the AddDirectory() overload that /// allows you to explicitly specify a directory path for use in the archive. /// </para> /// /// <para> /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see /// cref="Password"/>, <see cref="SetCompression"/>, <see /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>, /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their /// respective values at the time of this call will be applied to each /// ZipEntry added. /// </para> /// /// </remarks> /// /// <seealso cref="Ionic.Zip.ZipFile.AddItem(string)"/> /// <seealso cref="Ionic.Zip.ZipFile.AddFile(string)"/> /// <seealso cref="Ionic.Zip.ZipFile.UpdateDirectory(string)"/> /// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string, string)"/> /// /// <overloads>This method has 2 overloads.</overloads> /// /// <param name="directoryName">The name of the directory to add.</param> /// <returns>The <c>ZipEntry</c> added.</returns> public static ZipEntry AddDirectory(this ZipFile zipFile, string directoryName) { return zipFile.AddDirectory(directoryName, null); }