Welcome to the Xceed Community | Help
Community Search  
More Search Options

Xceed ZIp Compression for Zipping Stream objects?

Sort Posts: Previous Next
  •  08-20-2009, 3:42 AM Post no. 23414

    Xceed ZIp Compression for Zipping Stream objects?

    Hi,

     Xceed Zip normally provides functionality to zip files. Is it possible that Xceed Zip can zip filestream objects?

    We are creating a stream object from a string (say) and want to add this stream to zip folder. Saving the stream in a temporary location and using that file for zipping process is not an option as this this operation is quite frequent.

    Please let us know if Xceed zip allows to zip file streams. Please explain if any pointers.

     Regards,

    Jyoti Prakash Dash

  •  08-20-2009, 4:21 PM Post no. 23442 in reply to 23414

    Re: Xceed ZIp Compression for Zipping Stream objects?

    Hi Jyoti,

    You could create a zip archive while streaming by using the ZipWriter that wraps your Stream, fileStream1, class as follow:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Diagnostics;
    using Xceed.Zip.ReaderWriter;

    namespace WriteToZip
    {
      class Program
      {
        static void Main(string[] args)
        {
          if (args.Length > 2)
          {
            string tempPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            using (FileStream fileStream1 = new FileStream(tempPath + @"\" + args[0], FileMode.Create, FileAccess.Write))
            {
              //Create the ZipWriter object around the stream.
              Xceed.Zip.ReaderWriter.ZipWriter zipWriter1 =
                new Xceed.Zip.ReaderWriter.ZipWriter(fileStream1);

              //Create ZipItemLocalHeader for current item and write to archive.
              ZipItemLocalHeader zipItemLocalHeader1 = new ZipItemLocalHeader
                ("zipItem.txt",Xceed.Compression.CompressionMethod.Deflated,Xceed.Compression.CompressionLevel.Highest);
              zipWriter1.WriteItemLocalHeader(zipItemLocalHeader1);

              byte[] buffer = new byte[1024];
              int bufferIndex = 0;
              for (int words = 1; words < args.Length; words++)
              {
                for (int i = 0; i < args[words].Length; i++)
                  buffer[bufferIndex++] = (byte)args[words]Idea;
                if(words<args.Length-1)
                  buffer[bufferIndex++] = 32;
              }
              int read = 0;
              //Write the current item's data to the Zip archive
              zipWriter1.WriteItemData(buffer, 0, bufferIndex);
              Console.WriteLine("Writing {0}. {1} bytes written.",
                zipItemLocalHeader1.FileName, bufferIndex);
              //Close the Zip archive. Writes the archive's central header.
              zipWriter1.CloseZipFile();
              Console.WriteLine("Zip archive created.");
            }
          } else
          {
            Console.WriteLine("Usage: " + Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName) + " ZipFileToCreate.Zip TextToEncode");
          }
        }
      }
    }


    Xceed - Software Developer and Technical Support
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.