Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need your help to understand which is the best way to compose multiple Stream objects in C#. Please think about the following scenario: I have a file on disk (no matter the file type; think about it as a byte array). I want to read this file, compress it, encrypt it and then write it to another file or send it on the network.
My question is: what's the best way to accomplish this without reading the file, compress and write it in a temp file, then read the temp file, encrypt it, and write to the destination file?
Edit
#itsme86: using a byte array is not a good idea for my purpose as the file could have a big size and generate memory problems.
#Xerillio: naturally I already tried to use temp file, but it's not a good solution: longer processing times and a large amount of disk space.
#Stewart Ritchie: yes, this is the idea, but I don't want to have memory problems, so I'd like to compose the streams and write the result directly on the final "support".
Thank you,
Attilio
If you want to compress a stream, use something like GZipStream.
https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.gzipstream?view=netcore-3.1
using (FileStream originalFileStream = fileToCompress.OpenRead())
using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
{
originalFileStream.CopyTo(compressionStream);
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I want to edit raw data from files (Hex) but i don't know how can i do it
Is there any package that can help me?
Also how do i convert strings in raw bytes?
I've tried some solutions out of google but none of these worked
Here's a function that:
Reads all the bytes from a file
Changes the second byte to the decimal value 8
Writes the bytes back to the file
Note, this is not the most efficient way to do this, but it's an example.
public async Task ChangeSomeBytesInFileAsync(string path)
{
var bytes = await File.ReadAllBytesAsync(path);
bytes[2] = 0b0000_01000;
await File.WriteAllBytesAsync(path, bytes);
}
To get the bytes of a string use the Encoding class and the appropriate encoding In this example, I'm using UTF-8.
var bytes = Encoding.UTF8.GetBytes(#"some value");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i have 46 GB TXT File
i need to remove duplicate and other method
i think need to load into memory and haven't enough space available...
anyone have a method to load 46 GB TXT File as List<T> or object on 4GB RAM as Virtual Memory or Memory Map File?
No need speed or performance!
Open the file with random access, but read it sequentially. Whenever you read a whole line calculate a hash code of this line, and check if you already calculated this hash code. Store the hash code, together with file offset of the line in a MultiValueDictionary (hash code as key, file offset as value). So whenever you read a new line and calculate its hash code you check if you already have the hash code. If you have it then you also must verify that lines match byte for byte (and you have one or more file offsets of previous lines, so if one of those lines match the current line byte for byte, then you have a duplicate).
If you have a duplicate line, then just continue reading. If you have a new line then write the line in a new file. This new file will contain only unique lines in the end.
You will have to remember your progress with the original file, because you will go back and forth. Back to compare old lines, forth to read new lines.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to convert .wav audio file to byte and then convert this byte array to string.
I do this using c# but the string it return is not in valid format i.e. not in readable form.
How can i get the string in readable format?
var bytes = File.ReadAllBytes("SomeAudio.wav");
string result = System.Text.Encoding.UTF8.GetString(bytes);
In result the data is not readable i.e. it returns �𣻰��𠀁걄𫄐�𣻌
You should use System.Text.Encoding.ASCII instead
var bytes = File.ReadAllBytes("SomeAudio.wav");
string result = System.Text.Encoding.ASCII.GetString(bytes)
and for write some sample you can use
FileStream f = new FileStream("a.wav", FileMode.Create);
BinaryWriter wr = new BinaryWriter(f);
wr.Write(System.Text.Encoding.ASCII.GetBytes("RIFF"));
If you want to create blob here is good example "MSSQL" later you can put blob to table in database.
DECLARE #BLOB varbinary(MAX)
SET #BLOB = (SELECT *
FROM OPENROWSET(BULK 'C:\Users\AAAAAAA\Desktop\master\templates\miusic.wav', SINGLE_BLOB) AS BLOB)
SELECT #BLOB
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm loading JSON.net to load a JSon file, then I need to find some "target/keywords" that I have to delete and finaly resaved it.
So, I need to load the file, find the keyword, delete the section related to this keyword and save the file back with this modification.
I use the very last version of JSON.Net.
Any idea ?
Here is my input :
1) file name
2) keyword, by example : dns_prefetching/host_referral_list
Some kind of files I try to modify are the google chrome files, like :
%localappdata%\Google\Chrome\User Data\Default\Preferences
localappdata%\Google\Chrome\User Data\Local State
Thanks
Use JObject. You can then index with the name of a property and you can use Remove to remove tokens. For example:
string json = #"{CPU: 'Intel',Drives: [ 'DVD read/writer', '500 gigabyte hard drive' ]}";
JObject o = JObject.Parse(json);
var d = o["Drives"];
Console.WriteLine(d); // outputs DVD read/writer and 500 gigabyte hard drive
d[0].Remove();
Console.WriteLine(d); // outputs only 500 gigabyte hard drive
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
A similar question has already been asked on this thread:
Save JPG in progressive format
However, the answer marked is basically saying that it can't be done. However, considering it is done using other software, it definitely is something which C# can do.
Is there any way this can be done?
Magick.NET can do it with a code like this
using (MagickImage image = new MagickImage("input.png"))
{
// Set the format and write to a stream so ImageMagick won't detect the file type.
image.Format = MagickFormat.Pjpeg;
using (FileStream fs = new FileStream("output.jpg", FileMode.Create))
{
image.Write(fs);
}
// Write to .jpg file
image.Write("PJEG:output.jpg");
// Or to a .pjpeg file
image.Write("output.pjpg");
}
You can find more details here.