Új hozzászólás Aktív témák
-
joysefke
veterán
Legyen szíves valaki segítsen. System.IO.Compression-al bohóckodom, biztosan valami bufferprobléma van, de nem látom, hogy hol... Elég béna vagyok

Itt van két nyúlfarknyi kód, az egyikben nincs tömörítés és működik, a másikban van tömörítés és nem működik. Az írásnál nem futok bele exceptionba.
Itt alább ez működik:
Gyártok egy string[] tömböt és elemenként mint szöveget beleírom egy StreamWriter(new MemoryStream)-be. Majd ennek az ellentettje, mindent szépen rendben vissza tud olvasni. Egyszer 1000 utána 10000 elemet próbálok tömöríteni, kiírni.Előre is köszönöm!!!
using System;
using System.IO;
class Program
{
//Gets a string[] array with n data items
public static string[] GetNumbers(int n)
{
string[] numbers = new string[n];
for (int i = 0; i < n; ++i) numbers[i] = (i + 20000).ToString();
return numbers;
}
// writes the string[] array consisting of n items as text to an underlying memory stream
// after that the backing byte[] data structure of the memory stream is extracted via toArray()
// this byte[] array is used to instantitze a MemoryStream and read back the content as text
// this works as expected
public static void WriteReadStrings(int n)
{
string[] numbers = GetNumbers(n);
byte[] data;
using (MemoryStream ms = new MemoryStream())
using (StreamWriter sw = new StreamWriter(ms))
{
int i = -1;
try
{
for (i = 0; i < n; ++i) sw.WriteLine(numbers[i]);
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
Console.WriteLine("Current item: {0} \r\nhit key!", i);
Console.ReadKey();
}
sw.Flush();
ms.Flush();
data = ms.ToArray();
}
Console.WriteLine("data size: {0}", data.Length);
Console.WriteLine("hit key to proceed");
Console.ReadKey(true);
//reading back the compressed stream
using (MemoryStream ms = new MemoryStream(data))
using (StreamReader sr = new StreamReader(ms))
{
while (sr.EndOfStream != true) Console.WriteLine(sr.ReadLine());
}
}
static void Main(string[] args)
{
WriteReadStrings(1000);
Console.WriteLine("hit key to proceed");
Console.ReadKey(true);
WriteReadStrings(10000);
Console.WriteLine("hit key to proceed");
Console.ReadKey(true);
}
}Ok, és most ugyanez tömörítéssel:
Totálisan nem megy. Ha 1000 elemet próbálok tömöríteni, semmi nem kerül bele a MemoryStreambe, ha 10000-et, akkor meg csak valami 8000 környéki. Biztosan valami buffer hiba van, de hol???using System;
using System.IO;
using System.IO.Compression;
class Program
{
//Gets a string[] array with n data items
public static string[] GetNumbers(int n)
{
string[] numbers = new string[n];
for (int i = 0; i < n; ++i) numbers[i] = (i + 20000).ToString();
return numbers;
}
// writes the string[] array consisting of n items as text compressed to an underlying memory stream
// after that the backing byte[] data structure of the memory stream is extracted via toArray()
// this byte[] array is used to instantitze a MemoryStream and read back the content as text
// this does NOT work
public static void CompressAndDecompressStringArr(int n)
{
string[] numbers = GetNumbers(n);
byte[] compressedData;
using (MemoryStream ms = new MemoryStream())
using (GZipStream ds = new GZipStream(ms, CompressionMode.Compress))
using (StreamWriter sw = new StreamWriter(ds))
{
int i = -1;
try
{
for (i = 0; i < n; ++i) sw.WriteLine(numbers[i]);
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
Console.WriteLine("Current item: {0} \r\nhit key!", i);
Console.ReadKey();
}
sw.Flush();
ds.Flush();
ms.Flush();
compressedData = ms.ToArray();
}
Console.WriteLine("Compressed data size: {0}", compressedData.Length);
Console.WriteLine("hit key to proceed");
Console.ReadKey(true);
//reading back the compressed stream
using (MemoryStream ms = new MemoryStream(compressedData))
using (GZipStream ds = new GZipStream(ms, CompressionMode.Decompress))
using (StreamReader sr = new StreamReader(ds))
{
while (sr.EndOfStream != true) Console.WriteLine(sr.ReadLine());
}
using (MemoryStream ms = new MemoryStream(compressedData))
using (GZipStream ds = new GZipStream(ms, CompressionMode.Decompress))
using (StreamReader sr = new StreamReader(ds))
{
string[] separator = new string[1];
separator[0] = Environment.NewLine;
Console.WriteLine("Split returned {0} lines", sr.ReadToEnd().Split(separator, StringSplitOptions.RemoveEmptyEntries).Length );
Console.WriteLine("hit key to proceed");
Console.ReadKey(true);
}
}
static void Main(string[] args)
{
CompressAndDecompressStringArr(1000);
Console.WriteLine("hit key to proceed");
Console.ReadKey(true);
CompressAndDecompressStringArr(10000);
Console.WriteLine("hit key to proceed");
Console.ReadKey(true);
}
}
Új hozzászólás Aktív témák
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- Nvidia GeForce RTX 3070 Founders Edition 8GB, Garanciával!
- Új, Gamer félgép - GIGABYTE B850 GAMING X WiFi + Ryzen 7 9800X3D + Corsair 2x16GB DDR5 6000MHz
- Újszerű MSI Thin 15 - 15.6"FHD 144Hz - i5 -13420H - 16GB - 512GB - Win11- RTX 3050 - 2+ év garancia
- Intel Core I9 14900KS - 24mag/32szál - Eladó!
- Apple TV 4K 2021 32 GB (A2169)
- Lenovo X395 Ryzen 5 pro 3500U, 16GB RAM, SSD, jó akku, számla, garancia
- GYÖNYÖRŰ iPhone 14 Pro 128GB Space Black-1 ÉV GARANCIA - Kártyafüggetlen, MS3781
- Gamer PC-Számítógép! Csere-Beszámítás! I5 9600KF / RTX 3060Ti / 16GB DDR4 / 256SSD + 2TB HDD
- Apple iPhone 17 256GB & 512GB Bontatlan Független Összes Szín / 27% áfás ár
- HP ProBook 445 G8 14" Ryzen 5 5600U, 8-16GB RAM, 256-512GB SSD, jó akku, számla, 6 hó gar
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest

