Can anyone explain me about the following code what it is doing and
why it is substracting the size by 44.
public byte[] recorded_file = new byte[1000000];
public bool recorded_first_stream;
public int recorded_file_location;
public async void TestFile(object object_name)
{
string file_name = (string)object_name;
string full_file_name = System.IO.Path.Combine
(Properties.Settings.Default.wave_files_location, file_name);
if (File.Exists(full_file_name))
{
// WaveStream fileStream = new AudioFileReader(full_file_name);
recorded_first_stream = true;
FileStream fs = File.Open(full_file_name, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
recorded_file_location = (int)new System.IO.FileInfo(full_file_name).Length - 44;
BinaryReader wave_file = new BinaryReader(fs);
wave_file.BaseStream.Seek(44, SeekOrigin.Begin);
byte[] wave_bytes = wave_file.ReadBytes(1000000);
Buffer.BlockCopy(wave_bytes, 0, recorded_file, 0, wave_bytes.Length);
}
Related
Because the maximum value of a byte array is 2GB, lets say i have a larger file and i need to convert it to a byte array. Since i can't hold the whole file, how should i convert it into two?
I tried:
long length = new System.IO.FileInfo(#"c:\a.mp4").Length;
int chunkSize = Convert.ToInt32(length / 2);
byte[] part2;
FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
try
{
part2 = new byte[chunkSize]; // create buffer
fileStream.Read(part2, 0, chunkSize);
}
finally
{
fileStream.Close();
}
byte[] part3;
fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
try
{
part3 = new byte[chunkSize]; // create buffer
fileStream.Read(part3, 5, (int)(length - (long)chunkSize));
}
finally
{
fileStream.Close();
}
but it's not working.
Any ideas?
You can use a StreamReader to read in file too large to read into a byte array
const int max = 1024*1024;
public void ReadALargeFile(string file, int start = 0)
{
FileStream fileStream = new FileStream(file, FileMode.Open,FileAccess.Read);
using (fileStream)
{
byte[] buffer = new byte[max];
fileStream.Seek(start, SeekOrigin.Begin);
int bytesRead = fileStream.Read(buffer, start, max);
while(bytesRead > 0)
{
DoSomething(buffer, bytesRead);
bytesRead = fileStream.Read(buffer, start, max);
}
}
}
If you are working with extremely large files, you should use MemoryMappedFile, which maps a physical file to a memory space:
using (var mmf = MemoryMappedFile.CreateFromFile(#"c:\path\to\big.file"))
{
using (var accessor = mmf.CreateViewAccessor())
{
byte myValue = accessor.ReadByte(someOffset);
accessor.Write((byte)someValue);
}
}
See also: MemoryMappedViewAccessor
You can also read/write chunks of the file with the different methods in MemoryMappedViewAccessor.
This was my solution:
byte[] part1;
byte[] part2;
bool odd = false;
int chunkSize = Convert.ToInt32(length/2);
if (length % 2 == 0)
{
part1 = new byte[chunkSize];
part2 = new byte[chunkSize];
}
else
{
part1 = new byte[chunkSize];
part2 = new byte[chunkSize + 1];
odd = true;
}
FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
using (fileStream)
{
fileStream.Seek(0, SeekOrigin.Begin);
int bytesRead = fileStream.Read(part1, 0, chunkSize);
if (odd)
{
bytesRead = fileStream.Read(part2, 0, chunkSize + 1);
}
else
{
bytesRead = fileStream.Read(part2, 0, chunkSize);
}
}
SQLiteConnection.Open does not throw exception when opening a file that is not a database.
private void openDatabase()
{
sqlite = new SQLiteConnection("Data Source=" + this.filePath + ";Version=3;");
try
{
sqlite.Open();
}
catch(SQLiteException e)
{
MessageBox.Show(e.Message + e.StackTrace);
}
}
How do I determine if a file is a SQLite Database?
Read the first 16 bytes and then check for the string "SQLite Format"
VB.Net
Dim bytes(16) As Byte
Using fs As New IO.FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
fs.Read(bytes, 0, 16)
End Using
Dim chkStr As String = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
Return chkStr.Contains("SQLite format")
Update 2
C#
byte[] bytes = new byte[17];
using (IO.FileStream fs = new IO.FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
fs.Read(bytes, 0, 16);
}
string chkStr = System.Text.ASCIIEncoding.ASCII.GetString(bytes);
return chkStr.Contains("SQLite format");
public static bool isSQLiteDatabase(string pathToFile)
{
bool result = false;
if (File.Exists(pathToFile)) {
using (FileStream stream = new FileStream(pathToFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
byte[] header = new byte[16];
for (int i = 0; i < 16; i++)
{
header[i] = (byte)stream.ReadByte();
}
result = System.Text.Encoding.UTF8.GetString(header).Contains("SQLite format 3");
stream.Close();
}
}
return result;
}
I have an image in ushort variable, want to save this image in binary format.
Please, anyone, tell me How can this be done using C#?
I have tried this but its not working
ushort[] Depthdata;
Depthdata = new ushort[DWidth * DHeight];
string s1 = string.Format("{0}", count_depth);
FileStream fs = new FileStream("C:\\img" + s1 + ".bin", FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
string image_str = Convert.ToString(Imagedata);
bw.Write(image_str);
bw.Close();
fs.Close();
Here is attached my full code
I would like to mention that the code here, and the one in your link are different...
In any case, going by the one in your link:
ushort[] Depthdata;
....
string s1 = string.Format("{0}", count_depth);
FileStream fs = new FileStream("G:\\test" + s1 + ".bin", FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
string depth_str = Convert.ToString(Depthdata);
bw.Write(depth_str);
bw.Close();
fs.Close();
You shouldn't actually need to convert your Depthdata to a string. BinaryWriter can actually take a ushort value in one of its overloads. Why not just iterate through and write it out? Also, you should use using statements for your filestream and binarywriter.
Try the following:
using(FileStream fs = new FileStream("G:\\test" + s1 + ".bin", FileMode.Create, FileAccess.Write))
{
using(BinaryWriter bw = new BinaryWriter(fs))
{
foreach(ushort value in Depthdata)
{
bw.write(value);
}
}
}
I think this will help you.i tested this on *.tiff file
first make separate Class Ext
public static class Ext
{
public static string ToHexString(this byte[] hex)
{
if (hex == null) return null;
if (hex.Length == 0) return string.Empty;
var s = new StringBuilder();
foreach (byte b in hex)
{
s.Append(b.ToString("x2"));
}
return s.ToString().ToUpper();
}
}
then you can Add following code to convert image to string binary
FileStream fs=new FileStream(ImgPathID, FileMode.Open, FileAccess.Read); //set file stream
Byte[] bindata=new byte[Convert.ToInt32(fs.Length)];
fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
string bindatastring = Ext.ToHexString(bindata);// call to class
I am trying to send a FileStream of a file.
But I now want to add 40 byte Checksum to the start.
How can I do this? Ive tried creating my own stream class to concatinate two streams.. And Ive looked at stream writers.
Surely they must be an easy way. Or an alternative way. And I DONT want to load the entire file into a byte array, appead to that and write that back to a stream.
public Stream getFile(String basePath, String path) {
return new FileStream(basePath + path, FileMode.Open, FileAccess.Read);
}
See MergeStream.cs. Here's how you can use it:
var mergeStream = new MergeStream(new MemoryStream(checksum), File.OpenRead(path));
return mergeStream;
byte[] checksum = new byte[40];
//...
FileStream oldFileStream = new FileStream(oldFile, FileMode.Open, FileAccess.Read);
FileStream newFileStream = new FileStream(newFile, FileMode.Create, FileAccess.Write);
using(oldFileStream)
using(newFileStream)
{
newFileStream.Write(checksum, 0, checksum.Length);
oldFileStream.CopyTo(newFileStream);
}
File.Copy(newFile, oldFile, overwrite : true);
If you don't want to use a temporary file, the only solution is to open the file in ReadWrite mode and use two alternating buffers:
private static void Swap<T>(ref T obj1, ref T obj2)
{
T tmp = obj1;
obj1 = obj2;
obj2 = tmp;
}
public static void PrependToFile(string filename, byte[] bytes)
{
FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
PrependToStream(stream, bytes);
}
public static void PrependToStream(Stream stream, byte[] bytes)
{
const int MAX_BUFFER_SIZE = 4096;
using(stream)
{
int bufferSize = Math.Max(MAX_BUFFER_SIZE, bytes.Length);
byte[] buffer1 = new byte[bufferSize];
byte[] buffer2 = new byte[bufferSize];
int readCount1;
int readCount2;
long totalLength = stream.Length + bytes.Length;
readCount1 = stream.Read(buffer1, 0, bytes.Length);
stream.Position = 0;
stream.Write(bytes, 0, bytes.Length);
int written = bytes.Length;
while (written < totalLength)
{
readCount2 = stream.Read(buffer2, 0, buffer2.Length);
stream.Position -= readCount2;
stream.Write(buffer1, 0, readCount1);
written += readCount1;
Swap(ref buffer1, ref buffer2);
Swap(ref readCount1, ref readCount2);
}
}
}
I have problem with decomress gzip:
string fileData = string.Empty;
// byte[] starts with 31 and 139
var gzBuffer = entity.Data.Skip(pos).ToArray();
using (GZipStream stream = new GZipStream(new MemoryStream(gzBuffer),CompressionMode.Decompress))
{
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
} while (count > 0);
fileData = Encoding.UTF8.GetString(memory.ToArray());
}
}
In the debugger, count allways equal 0. Where is the problem?
Thanks.
not sure if this will be of much help to you, but I will try. This is what I used in a sample project to compress/decompress files with GZIP. Maybe you can adapt this code for your needs?
public string Compress(FileInfo fi)
{
string outPath;
using (FileStream inFile = fi.OpenRead())
{
outPath = fi.FullName + ".gz";
using (FileStream outFile = File.Create(outPath))
{
using (var compress = new GZipStream(outFile,
CompressionMode.Compress))
{
inFile.CopyTo(compress);
}
}
}
return outPath;
}
public void Decompress(FileInfo fi)
{
using (FileStream inFile = fi.OpenRead())
{
string curFile = fi.FullName;
string origName = curFile.Remove(curFile.Length - fi.Extension.Length);
using (FileStream outFile = File.Create(origName))
{
using (var decompress = new GZipStream(inFile,
CompressionMode.Decompress))
{
decompress.CopyTo(outFile);
}
}
}
}