Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Creating Email Attachment with Stream object.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #20074 |

    I was wondering if it was possible to create a zip file from a
    Stream and pass it to the System.Net.Mail.Attachment class as a
    Stream.  Our goal is to not write anything to the disk.  I am unsure of
    how to do this because all the examples of using the zip libraries with
    Stream classes also use DiskFile.

     

    Thanks

    Imported from legacy forums. Posted by Anthony (had 3783 views)

    User (Old forums)
    Member
    Post count: 23064

    I was able to get this working using the following code:

     

                var streamFile = new StreamFile(contentStream, file.Name);
                var zipFile = new MemoryFile();
                var archive = new ZipArchive(zipFile) {
                    DefaultCompressionMethod = CompressionMethod.Deflated,
                    DefaultEncryptionMethod = EncryptionMethod.Compatible,
                    AllowSpanning = true
                };
                streamFile.CopyTo(archive, true);
                return new Attachment(zipFile.OpenRead(), “attachment.zip”);

     

    I am running into problems now using the memoryfile when I want to split the zip archive.  How do I get back each segment of the split zip file in a stream?

     

    Thanks

     

    Imported from legacy forums. Posted by Anthony (had 961 views)

    User (Old forums)
    Member
    Post count: 23064

    I assume you want to create a split archive, so you can send it in multiple pieces through multiple emails.

    First, in this case, what you need is a split file, not a span file.  A span file is for removable disks, and in your case, MemoryFile is not a removable file, so it will not span.  Set the SplitSize property on ZipArchive instead, along with the SplitNameFormat property.

    Second, you could use a MemoryFolder into which to place the MemoryFile, so that when the archive is built, all file parts are added to this folder.  Then call GetFiles() on the folder, and use each file that is returned to create a corresponding attachment, specifying the file name with the right extension for each (e.g. zip, z01, z02,…).

    Imported from legacy forums. Posted by André (had 552 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks for your help.  This was it ended up looking like…     

     

                var streamFile = new StreamFile(contentStream, name);
                var rootFolder = Guid.NewGuid().ToString();

                var zipFile = new MemoryFile(rootFolder, name));
                var archive = new ZipArchive(zipFile) {
                    DefaultCompressionMethod = CompressionMethod.Deflated,
                    DefaultEncryptionMethod = EncryptionMethod.Compatible,
                    AllowSpanning = true,
                    SplitNameFormat = SplitNameFormat.WinZip,
                    SplitSize = splitSize
                };

                streamFile.CopyTo(archive, true);
                var zipFolder = new MemoryFolder(rootFolder, @”\”);
                zipFolder.CopyFilesTo(new ZipEvents(), “”, archive, false, true, “*” + GetExtension(name));
                var streams = new List<Stream>();
                foreach (var item in zipFolder.GetFiles(true)) {
                    streams.Add(item.OpenRead());
                }
                return streams;

    Imported from legacy forums. Posted by Anthony (had 3703 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.