Quantcast
Channel: sharpcompress Discussions Rss Feed
Viewing all 239 articles
Browse latest View live

New Post: Writing files to a Zip using AddEntry/SaveTo: Streams are closed late.

$
0
0
I added entry.Close() as I was having the same issue. It fixed the problem, but is there a cleaner way to Add new entries to an existing Zip file instead of using:
ZipArchive and AddEntry? I checked the examples, but they seem to be a bit out of date.
Thanks!

New Post: Writing files to a Zip using AddEntry/SaveTo: Streams are closed late.

New Post: Support for empty directory

$
0
0
Hello,
I found sharpcompress will lost all empty directory when use the save to method.
And the archive loader of some format will not contains directory entry, like 7z.

Is there some reason sharpcompress not having support of directory entry?

New Post: SaveTo cannot call many time if there an entry added from memorystream.

$
0
0
I don't known it's a bug or not.
SaveTo cannot call many time if there an entry added from memorystream.
The second time file context with that entry will be empty,
I fix them by add these line in method OpenEntryStream
if (stream.CanSeek)
    stream.Seek(0, SeekOrigin.Begin);
is this right? will it cause some problem?

New Post: Support for empty directory

New Post: SaveTo cannot call many time if there an entry added from memorystream.

$
0
0
Where did you add it?

I think trying to reset Streams for everything will cause a problem. I'll take a closer look. I don't think I intended SaveTo to be called multiple times but I should do something about it.

New Post: SaveTo cannot call many time if there an entry added from memorystream.

$
0
0
Here is example
using (var arc = ZipArchive.Create())
{
    string str = "test.txt";
    var source = new MemoryStream(Encoding.UTF8.GetBytes(str));
    arc.AddEntry("test.txt", source, true, source.Length, null);
    arc.SaveTo(@"D:\a.zip", new CompressionInfo());
    arc.SaveTo(@"D:\b.zip", new CompressionInfo());
 }
test.txt in b.zip will be empty.

I think reset them only affect new files.
Old files always reset each time I call OpenEntryStream (or create a new stream? I didn't view so much code).
test code
var entry = arc.Entries.where(v => v.FilePath == "exist.txt");
Console.WriteLine(new StreamReader(entry.OpenEntryStream()).ReadToEnd());
Console.WriteLine(new StreamReader(entry.OpenEntryStream()).ReadToEnd());
Console.WriteLine(new StreamReader(entry.OpenEntryStream()).ReadToEnd());
if file is exist before open, the output of second and third time are same as first time.
if new added, second and third time will output empty string.

New Post: SaveTo cannot call many time if there an entry added from memorystream.

$
0
0
I believe I do create each Stream on the fly with OpenEntryStream as Archives require seekable streams. I guess the solution should be that Streams added to a writable archive must be seekable and therefore support your scenario.

Will code more.

New Post: SaveTo cannot call many time if there an entry added from memorystream.

New Post: Going into subdirectories when extracting?

$
0
0
When enumerating entries, the directory doesn't matter. It should see entries as directory\file3 and directory\file4

New Post: Can make method AddEntry return the entry added?

$
0
0
Hello,
I'm trying to manage the file tree outside of sharpcompress.
I found method AddEntry will return nothing so I had to enumerate Entries each time after I add a file.
Then I make a patch
diff --git a/SharpCompress/Archive/AbstractWritableArchive.cs b/SharpCompress/Archive/AbstractWritableArchive.cs
index c03f549..8e3b276 100644
--- a/SharpCompress/Archive/AbstractWritableArchive.cs
+++ b/SharpCompress/Archive/AbstractWritableArchive.cs
@@ -67,28 +67,32 @@ namespace SharpCompress.Archive
             }
         }
 
-        public void AddEntry(string filePath, Stream source,
+        public TEntry AddEntry(string filePath, Stream source,
                              long size = 0, DateTime? modified = null)
         {
-            newEntries.Add(CreateEntry(filePath, source, size, modified, false));
+            var entry = CreateEntry(filePath, source, size, modified, false);
+            newEntries.Add(entry);
             RebuildModifiedCollection();
+            return entry;
         }
 
-        public void AddEntry(string filePath, Stream source, bool closeStream,
+        public TEntry AddEntry(string filePath, Stream source, bool closeStream,
                              long size = 0, DateTime? modified = null)
         {
-            newEntries.Add(CreateEntry(filePath, source, size, modified, closeStream));
+            var entry = CreateEntry(filePath, source, size, modified, closeStream);
+            newEntries.Add(entry);
             RebuildModifiedCollection();
+            return entry;
         }
 
 #if !PORTABLE && !NETFX_CORE
-        public void AddEntry(string filePath, FileInfo fileInfo)
+        public TEntry AddEntry(string filePath, FileInfo fileInfo)
         {
             if (!fileInfo.Exists)
             {
                 throw new ArgumentException("FileInfo does not exist.");
             }
-            AddEntry(filePath, fileInfo.OpenRead(), true, fileInfo.Length, fileInfo.LastWriteTime);
+            return AddEntry(filePath, fileInfo.OpenRead(), true, fileInfo.Length, fileInfo.LastWriteTime);
         }
 #endif
If you think it's reasonable please merge it, thank you for the work.

New Post: SaveTo cannot call many time if there an entry added from memorystream.

$
0
0
I found this problem isn't completely solved yet.
If two new files use a same stream, the second file content will be empty after save.
example code:
using (var arc = ZipArchive.Create())
{
    using (var stream = new MemoryStream(Encoding.UTF8.GetBytes("qwert")))
    {
        arc.AddEntry("1.txt", stream, false, stream.Length, null);
        arc.AddEntry("2.txt", stream, false, stream.Length, null);
        arc.SaveTo(@"d:\1.zip", new CompressionInfo());
        arc.SaveTo(@"d:\2.zip", new CompressionInfo());
    }
}

New Post: Can make method AddEntry return the entry added?

New Post: SaveTo cannot call many time if there an entry added from memorystream.

New Post: NotImplementedException throwed when read a stream

$
0
0
adamhathcock wrote:
I guess ReadToEnd requires length to properly function. GZipStream is a forward-only stream implementation so length won't necessarily be known (like if you're using a NetworkStream).

Can you do without ReadToEnd?
This error is very weird. If I bound the above code in try-catch statement, I will still get my expected results (the exception still happened). This error only happened in Windows Phone 7, and dis not happen in Windows Phone 8 and Windows Store app.

New Post: NotImplementedException throwed when read a stream

$
0
0
I'm not sure where that AsyncExtensions class comes from. Maybe that's the Windows Phone 7 SDK that I don't currently use or have anymore.

Looking at the .NET 4.5 implementation of StreamReader.ReadToEndAsync, it doesn't look like they use Length either. What determines the end of a stream is if any bytes are returned on a Stream.Read.

New Post: NotImplementedException throwed when read a stream

New Post: NotImplementedException throwed when read a stream

$
0
0
adamhathcock wrote:
It must be the Microsoft.Bcl.Async package doing that https://www.nuget.org/packages/Microsoft.Bcl.Async
Yes, I am currently using the Microsoft.Bcl.Async package along with the SharpCompress. But I dont think this error is related to the Microsoft.Bcl.Async package. I tried to using ReadToEnd method instead of ReadToEndAsync but I got same error.

Note: The above code is written in a portable class library which is configured to run with the following frameworks: .NET 4.5, Silverlight 4+, Windows Phone 7.5+ and Windows store apps.

New Post: How to decompress a compressed string?

$
0
0
Hi everyone. I managed to compress a string with the following method:
public string SharpZip(string asd)
        {
            byte[] sIn = UTF8Encoding.UTF8.GetBytes(asd);

            MemoryStream rawDataStream = new MemoryStream();
            SharpCompress.Compressor.Deflate.DeflateStream gzipOut = new SharpCompress.Compressor.Deflate.DeflateStream(rawDataStream, SharpCompress.Compressor.CompressionMode.Compress);

            gzipOut.Write(sIn, 0, sIn.Length);
            gzipOut.Close();

            byte[] compressed = rawDataStream.ToArray();

            // data sent to the php service
            return Convert.ToBase64String(compressed);
        }
But I'm having trouble to decompress the string. I made various attemps with no luck, being the last one this:
public string SharpUnzip(string asd)
        {
            byte[] sOut = UTF8Encoding.UTF8.GetBytes(asd);
            MemoryStream rawDataStream = new MemoryStream();
            SharpCompress.Compressor.Deflate.DeflateStream gzipOut = new SharpCompress.Compressor.Deflate.DeflateStream(rawDataStream, SharpCompress.Compressor.CompressionMode.Decompress);

            gzipOut.Read(sOut, 0, sOut.Length);
            gzipOut.Close();

            using (var streamReader = new StreamReader(rawDataStream))
            {
                Console.WriteLine(streamReader.ReadToEnd()); // Prints a blank line
            }

            return rawDataStream.ToString(); // Prints System.IO.MemoryStream
            //return "";
        }
Could anyone help me a little here? There doesn't seem to be any documentation on the matter...

Thanks!

New Post: How to decompress a compressed string?

$
0
0
This is just a basic .NET usage question. Nothing to do with SharpCompress.

However, the reason why this isn't working is because you ought to initialize your MemoryStream with the byte array (sOut). Then wrap the MemoryStream with the DeflateStream then pass the DeflateStream to the StreamReader.
Viewing all 239 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>