Read inside .DAT file using C# - c#

I have .DAT from SharePoint, to recover some of the data I need to read the .DAT file using C#.
Some of the options are
StreamReader objInput = new StreamReader(filename, System.Text.Encoding.Default);
string contents = objInput.ReadToEnd().Trim();
string[] split = System.Text.RegularExpressions.Regex.Split(contents, "\\s+", RegexOptions.None);
foreach (string s in split)
{
Console.WriteLine(s);
}
or
//ObjectToSerialize objectToSerialize;
//Stream stream = File.Open(filename, FileMode.Open);
//BinaryFormatter bFormatter = new BinaryFormatter();
//objectToSerialize = (ObjectToSerialize)bFormatter.Deserialize(stream);
//stream.Close();
.../
The problem is the DAT file may contain XMl files, Doc files, or PPT or others. I just want list all the data and files inside the .DAT file.
Is there is any way I can do this is C#?

You can read .dat file using c#, but it depends on the structure of data how you have inside the .dat file
take a look at this link
How-read-data-from-DAT-file-using-C
how-can-i-read-data-from-dat-files

Related

Is possible to create zip password protected file without first creating file, then zip it

I am writing data into text file and using below code,
await using var file = new StreamWriter(filePath);
foreach (var packet in resultPackets)
{
file.WriteLine(JsonConvert.SerializeObject(packet));
}
And I am using below code to zip the file with password protected using `DotNetZip,
using (ZipFile zip = new ZipFile())
{
zip.Password = "password";
zip.AddFile(filePath);
zip.Save(#"C:\tmp\data4.zip");
}
Is there a way to combined both, I want to create a file on the fly as password protected.
I don't
want to create first file with data, t
then create zip file from it
and delete the base file
Is this possible? Thanks!
Okay, so since this is still unanswered, here's a small program that does the job for me:
using (var stream = new MemoryStream())
using (var streamWriter = new StreamWriter(stream))
{
// Insert your code in here, i.e.
//foreach (var packet in resultPackets)
//{
// streamWriter.WriteLine(JsonConvert.SerializeObject(packet));
//}
// ... instead I write a simple string.
streamWriter.Write("Hello World!");
// Make sure the contents from the StreamWriter are actually flushed into the stream, then seek the beginning of the stream.
streamWriter.Flush();
stream.Seek(0, SeekOrigin.Begin);
using (ZipFile zip = new ZipFile())
{
zip.Password = "password";
// Write the contents of the stream into a file that is called "test.txt"
zip.AddEntry("test.txt", stream);
// Save the archive.
zip.Save("test.zip");
}
}
Note how AddEntry does not create any form of temporary file. Instead, when the archive is saved, the contents of the stream are read and put into a compressed file within the archive. However, be aware that the whole content of the file are completely kept in memory before it the archive is written to the disk.

Contents of zip file are truncated

I want to write an XML string to a zip file using C#:
var myXmlString = "..."; // Contains some XML
// Set up zip archive
var archive = ZipFile.Open(FileName, ZipArchiveMode.Create);
var stream = archive.CreateEntry("myfile.xml").Open();
var sw = new StreamWriter(stream, Encoding.UTF8);
// Write data
sw.Write(myXmlString);
// Cleanup
stream.Flush();
stream.Close();
archive.Dispose();
This works fine except for one detail: the text in the zipped file is truncated. So the XML is just cut off at one point. If I extract the .xml file from the zip, it is 7.171 Bytes large. The XML string I wrote into the file is 7.404 Bytes long.
Can anyone help me find out where the missing bytes went?

convert compress xmldocument as zip and get as byte array

I am building a XmlDocument in memory (I am not writing it to disk). I need to be able to create a zip archive that would contain the Xml file and then get the zip archive as byte array (all of this without actually writing/creating anything on the hard-disk). Is this possible?
I should mention that I am trying to do this in C#.
var buffer = new MemoryStream();
using (buffer)
using (var zip = new ZipArchive(buffer, ZipArchiveMode.Create) )
{
var entry = zip.CreateEntry("content.xml", CompressionLevel.Optimal);
using (var stream = entry.Open())
{
xmlDoc.Save(stream);
}
}
var bytes = buffer.ToArray();

Zip files without inclusion of folders

I'm using System.IO.Compression in order to compress a file into a .zip, below the source code:
using (FileStream zipToOpen = new FileStream(zipName, FileMode.CreateNew)){
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)){
ZipArchiveEntry readmeEntry = archive.CreateEntry(#"C:\Users\soc\myFold\someFile.xml");
}
}
This piece of code works well, but unfortunately in the .zip there's the entire sequence of folders (C: -> Users -> ... -> someFile.xml); can I obtain a final .zip with ONLY the file I need? I know that with other libraries this fact is possible (DotNetZip add files without creating folders), but I would like to know if it were possible do the same with the standard library.
You seem to be under the impression that the file will be added to the archive, which is not the case. CreateEntry merely creates an entry with the specified path and entry name, you still need to write the actual file.
In fact, the code in your question is quite similar to the code in the documentation, so I assume you got it from there?
Anyway, to get only the file name you can use Path.GetFileName and then you can write the actual file content to the zip entry.
var filePath = #"C:\temp\foo.txt";
var zipName = #"C:\temp\foo.zip";
using (FileStream zipToOpen = new FileStream(zipName, FileMode.CreateNew))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
ZipArchiveEntry readmeEntry = archive.CreateEntry(Path.GetFileName(filePath));
using (StreamReader reader = new StreamReader(filePath))
using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
{
writer.Write(reader.ReadToEnd());
}
}
}
The code above will create an archive with foo.txt in the root and with the content of the source file, without any additional directories.

Writing a byte array as a file to a web location

private void CreateFile(byte[] outByteArray)
{
//Create a new file containing the returned PDF document
string filename = "C:\\myfile.pdf";
FileStream fs2 = new FileStream(filename, FileMode.OpenOrCreate);
BinaryWriter w = new BinaryWriter(fs2);
w.Write(outByteArray);
w.Close();
fs2.Close();
}
This is my current code.
I am writing to the server's drive. It is working fine.
But now I have to write the byte array to a web folder
I mean to some folder like http://tyyet.com/rsfgqs/destination/
and save it there in the name myfile.pdf.
How can I do that?
Can I use HttpWebRequest for that?

Categories