Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET How to write (C#) byte[] zippedBytes = ZipBytes(byte[] unzippedBytes, string entryName)

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

    I can’t find example for method that will create zippedBytes with one enty with the givenEntryName
    In a way that if the bytes are saved for example to c:\tmp.zip I will open the tmp.zip file and find the entry in it

     

    I’m using libraries for .net 3.5 

    Thanks

      Dekel

    Imported from legacy forums. Posted by Dekel (had 32 views)

    User (Old forums)
    Member
    Post count: 23064

    A bit cumbersome implementation:
    Is there a more elegant one?

            public static byte[] ZipBytes(byte[] ba, string entryName)
            {
                var memStream = new MemoryStream(ba);
                var streamFile = new StreamFile(memStream);
                streamFile.Name = entryName;

                var memFile = new MemoryFile(“MyMemFileRootName”, “MyMemFileFullName.zip”);
                streamFile.CopyTo(new ZipArchive(memFile), true);

                byte[] result = memFile.ToArray();

                memFile.Delete(); // Must call memFile.Delete, as it keeps static data (Is it using MemoryMappedFile?)
                return result;
            }

     My test code:

            private static void ZipTestByteArray()
            {
                byte[] ba = new byte[100];
                for (int i0 = 0; i0 < 100; i0++)
                    ba[i0] = (byte)i0;

                for (int i = 1; i < 3; i++)
                {
                    string targetPath = @”c:\tmp\tmp_” + i + “.zip”;
                    if (File.Exists(targetPath))
                        File.Delete(targetPath);
                    string entryName = “MyEntryName_” + i;

                    var ba2 = ZipBytes(ba, entryName);
                    File.WriteAllBytes(targetPath, ba2);

                }

                // Manual check – open the two files from file system
                // verify that the each file contains only one entry with correct name

            }
     

    Imported from legacy forums. Posted by Dekel (had 14 views)

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