Hirdetés
- Milyen videókártyát?
- Androidos tablet topic
- Emelkedik a korábbi generációs Intel CPU-k ára
- Kormányok / autós szimulátorok topikja
- Vezetékes FÜLhallgatók
- Brutálisan elszállhat az új PC-s Snapdragon fogyasztása
- VR topik (Oculus Rift, stb.)
- Milyen billentyűzetet vegyek?
- Ventilátorok - Ház, CPU (borda, radiátor), VGA
- AMD Ryzen 9 / 7 / 5 9***(X) "Zen 5" (AM5)
Ú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!
- Milyen videókártyát?
- Bemutatkozott a Poco X7 és X7 Pro
- Kínai és egyéb olcsó órák topikja
- Amazfit Active 2 NFC - jó kör
- The Game Awards 2025 - Íme a jelöltek listája
- Assetto Corsa Rally
- Kerékpárosok, bringások ide!
- Samsung Galaxy S23 Ultra - non plus ultra
- Androidos tablet topic
- Samsung Galaxy S25 Ultra - titán keret, acélos teljesítmény
- További aktív témák...
- DELL latitude 5410 Tartós Üzleti Laptop 14" -70% i5-10210U 4Mag 8Gb 256GB SSD FHD IPS
- DELL latitude 5410 Tartós Üzleti Laptop 14" -70% i5-8365U 4Mag 8Gb 256GB SSD FHD IPS
- Apple iPhone 13 Pro Max 128GB,Újszerű,Dobozával,12 hónap garanciával
- Apple iPhone 14 128GB,Újszerű,Dobozával,12 hónap garanciával
- Samsung Galaxy Tab S6 Lite 64GB,Átlagos,Dobozával,12 hónap garanciával
- Hibátlan, megkímélt! II Lenovo ThinkPad T540p II i7-4800MQ I GT 730M I 8GB I 240 GB I 15,6" FHD
- AKCIÓ! Apple Watch SE 2024 44mm Cellular okosóra garanciával hibátlan működéssel
- ÁRGARANCIA! Épített KomPhone Ultra 9 285K 64GB RAM RTX 5090 32GB GAMER PC termékbeszámítással
- BESZÁMÍTÁS! MSI B450M R5 5600X 32GB DDR4 1TB SSD RTX 4070 12GB Rampage SHIVA Cooler Master 750W
- Samsung UE75DU7172U 189 cm / 75 4K UHD Smart TV 6 hó garancia Házhozszállítás
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest



