Domanda

I've seen some answers to this question posted, but nothing exactly like what I am struggling with, and I'm having some trouble.

Basically, I am using an API that returns data in a byte array like so:

byte[] file = Api.getZippedReport(blah, blah);

I'm trying to figure out the best way to spit out the contents of the tab delimited file in C# so I can do something with it.

What is the simplest way to just get the data back so I can use it without actually having to save the file?

È stato utile?

Soluzione

In case this is a .net 4.5 application you can use the newly introduced ZipArchive class which offers a GetEntry() method:

Stream stream = new MemoryStream(file); // file as your byte[]
ZipArchive archive = new ZipArchive(stream )
ZipArchiveEntry entry = archive.GetEntry("ExistingFile.txt");

// Do your logic with the file you get from entry.Open()

entry.LastWriteTime = DateTimeOffset.UtcNow.LocalDateTime;

See ZipArchive Class and ZipArchive.GetEntry Method. There is a property on ZipArchive called Entries that contains all the entries in a readonly collection:

public ReadOnlyCollection<ZipArchiveEntry> Entries { get; }

Altri suggerimenti

the best way to spit out the contents of the tab delimited file in C#

byte[] file = Api.getZippedReport(blah, blah);
string fileString = System.Text.Encoding.UTF8.GetString(file);
string[] fileSplit = fileString.Split('\t');

Hope this helps... If it doesn't please let me know.

I ended up using the native .net 4.5 handler for zip files and it ended up looking like this:

    Stream stream = new MemoryStream(file); // file as your byte[]
    using (ZipArchive archive = new ZipArchive(stream))
    {
        foreach (ZipArchiveEntry entry in archive.Entries)
        {
            if (entry.FullName.EndsWith(".tsv", StringComparison.OrdinalIgnoreCase))
            {
                using (stream = entry.Open())
                using (var reader = new StreamReader(stream)) {
                        string output = reader.ReadToEnd();
            }
        }       
    }

This allowed me to get the file even though the filename changes dynamically. Hope this helps someone!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top