I encrypted a pdf and would like to save it to the database, below are the codes as to how I did it. I would like to know if there are any ways to set the Created "PDFEncrypted.pdf" onto the Stream fs without using a FileUploader?
string WorkingFolder = Server.MapPath("~/Files/");
string InputFile = Path.Combine(WorkingFolder, "PDF.pdf");
string OutputFile = Path.Combine(WorkingFolder, "PDFEncrypted.pdf");
using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(InputFile);
PdfEncryptor.Encrypt(reader, output, true, "pass123", "secret", PdfWriter.ALLOW_SCREENREADERS);
}
}
So I have these codes to upload it
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
Related
I want to read a binary file line by line (I'm writing of course continously, but I know that after 457 bytes new data start and I know exactly the byte structure and where which information is written to) and change a special entry of the line. I get an System.IO.IOException when I try to access the same file with both BinaryReader and BinaryWriter. I use locking to prevent that the file is accessed from somewhere else.
My code is:
using (FileStream fs2 = new FileStream(testfile, FileMode.Open, FileAccess.Read))
{
using (BinaryReader r = new BinaryReader(fs2))
{
using (BinaryWriter bw = new BinaryWriter(new FileStream(testfile, FileMode.Open, FileAccess.Write), utf8))
{
for (int i = 0; i < 11000; i+=457)
{
int myint = r.ReadInt64();
bw.Seek(i, SeekOrigin.Current);
bw.Write(myint*2);
}
}
}
}
How can I do this?
Do not create the second FileStream because the file is locked for the read operation by the first FileStream object.
If you are sure about file structure, the exception only can come out from 2nd FileStream instantiation. See link below for more information:
Read and Write to File at the same time
It is working for me using the following code:
if (File.Exists(testfile))
{
FileInfo fi = new FileInfo(testfile);
using (FileStream fs2 = new FileStream(testfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (BinaryReader r = new BinaryReader(fs2))
{
r.BaseStream.Seek(0, SeekOrigin.Begin);
using (BinaryWriter bw = new BinaryWriter(new FileStream(testfile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)))
{
for (int i = 0; i <= (fi.Length-177); i += 177)//181
{
}
}
}
}
}
This code will compress and serialize the object:
public static byte[] ObjectToByteArray(object[] obj)
{
using (MemoryStream msCompressed = new MemoryStream())
using (GZipStream gZipStream = new GZipStream(msCompressed, CompressionMode.Compress))
using (MemoryStream msDecompressed = new MemoryStream())
{
new BinaryFormatter().Serialize(msDecompressed, obj);
byte[] byteArray = msDecompressed.ToArray();
gZipStream.Write(byteArray, 0, byteArray.Length);
gZipStream.Close();
return msCompressed.ToArray();
}
}
And the following will upload it to the Azure Blob Storage:
byte[] byteObject = ObjectToByteArray(uploadObject);
using (Stream stream = new MemoryStream(byteObject))
{
stream.Seek(0, SeekOrigin.Begin);
blockBlob.UploadFromStream(stream, null, options);
}
This works great, but I can't find a way to download, decompress and deserialize this object/file from my storage.
You could use method DownloadToStream to download the file to local.
using (var fileStream = System.IO.File.OpenWrite(#"xxxx\compressedfile.gz"))
{
blockBlob.DownloadToStream(fileStream);
}
And then you could refer to the following code to decompress and deserialize the specified stream.
public static void DecompressAndDeserialize(string path)
{
using (FileStream originalFileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
FileInfo fileToDecompress = new FileInfo(path);
string FileName = fileToDecompress.FullName;
string newFileName = FileName.Remove(FileName.Length - fileToDecompress.Extension.Length);
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
}
}
FileStream fs = new FileStream(newFileName, FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
object[] uploadObject = (object[])formatter.Deserialize(fs);
}
}
/*Initialize FileStreams for both writing and reading*/
writeStream = new FileStream(logfilePath, FileMode.Append, FileAccess.Write, FileShare.Read);
readStream = new FileStream(logfilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
/*Initialize the Writer and Reader*/
fileWrite = new StreamWriter(writeStream);
fileRead = new StreamReader(readStream);
So this is what I use to be able to constantly write and read to the same file. How can I clear the file without the need to close both streams temporarily, clear file, and reopen them again?
I am writing the integer value in binary file as follows:-
int val =10;
FileStream fs = new FileStream("BinaryFile.bin", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs, Encoding.Unicode);
bw.Write(val);
//Reading value from binary as:-
FileStream fs = new FileStream("BinaryFile.bin", FileMode.Open);
BinaryReader br = new BinaryReader(fs, Encoding.Unicode);
int x = br.ReadInt32();
Value retrieved is: 1.092616E + 09
I am getting this value instead of '10'
Is there any other method to retrieve the int value?
Try by making change in BinaryWriter constructor
as
FileStream fs = new FileStream("iram.bin", FileMode.Create);
// Create the writer for data.
BinaryWriter w = new BinaryWriter(fs);
w.Write((int) 2000);
w.Close();
fs.Close();
and read using
using (FileStream fs2 = new FileStream("iram.bin", FileMode.Open))
{
using(BinaryReader r = new BinaryReader(fs2))
{
var integerValue = r.ReadInt32();
}
}
More detail Writing to .bin binary file
I have generated a pdf file with password protection by using the following code:
using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(input);
PdfEncryptor.Encrypt(reader, output, true, strDob, "secret", PdfWriter.ALLOW_SCREENREADERS);
}
}
I want to remove the password for the PDF file generated using the above code based on my certain condtions through code.
string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string InputFile = Path.Combine(WorkingFolder, "test.pdf");
string OutputFile = Path.Combine(WorkingFolder, "test_dec.pdf");//will be created automatically
//You must provide owner password but not the user password .
private void DecryptFile(string inputFile, string outputFile)
{
string password = #"secret"; // Your Key Here
try
{
PdfReader reader = new PdfReader(inputFile, new System.Text.ASCIIEncoding().GetBytes(password));
using (MemoryStream memoryStream = new MemoryStream())
{
PdfStamper stamper = new PdfStamper(reader, memoryStream);
stamper.Close();
reader.Close();
File.WriteAllBytes(outputFile, memoryStream.ToArray());
}
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
}