I don't know very well C# and compressed files, but i need to create comrpessed files with C# and read them with R. So which format should i use, that it is compatible with both C# and R?
I have tried this:
using (FileStream zipToOpen = new FileStream("myfile.gff", FileMode.Create))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
{
ZipArchiveEntry write_entry = archive.CreateEntry(path);
using (BinaryWriter writer = new BinaryWriter(write_entry.Open()))
{
//writing
}
}
}
but i can't open this file with the R function unzip().
EDIT:
zipF<-file.choose()
unzip(zipF)
Warning message: In unzip(zipF) : error 1 in extracting from zip file
Related
String fileName = "0001.gff";
using (FileStream zipToOpen = new FileStream(fileName, FileMode.Create))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
{
ZipArchiveEntry write_entry = archive.CreateEntry(fileName);
using (BinaryWriter writer = new BinaryWriter(write_entry.Open()))
{
writer.Write((Int32)1);
writer.Write((Int32)2);
writer.Write((Int32)3);
writer.Write((Int32)4);
}
}
}
I have created the file myFile in c#, but i want to read it in R. How can i do that?
There is a function called read.gff from the ape package the documentation says to try using
read.gff(file)
Here is the functionality I want to achieve
Write a JSON file.
Write a PDF file.
Create an archive for these two files
I am using the System.IO.Compression ZipArchive to achieve this. From the documentation, I have not found a good use case for this. The examples in documentation assume that the zip file exists.
What I want to do
Create zipArchive stream write JSON file and pdf file as entries in the zip file.
using (var stream = new FileStream(path, FileMode.Create))
{
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
{
ZipArchiveEntry manifest = archive.CreateEntry(filenameManifest);
using (StreamWriter writerManifest = new StreamWriter(manifest.Open()))
{
writerManifest.WriteLine(JSONObject_String);
}
ZipArchiveEntry pdfFile = archive.CreateEntry(filenameManifest);
using (StreamWriter writerPDF = new StreamWriter(pdfFile.Open()))
{
writerPDF.WriteLine(pdf);
}
}
}
You don't close the stream, you open with 'manifest.Open()'. Then it might not have written everything to the zip.
Wrap it in another using, like this:
using (var stream = new FileStream(path, FileMode.Create))
{
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
{
ZipArchiveEntry manifest = archive.CreateEntry(filenameManifest);
using (Stream st = manifest.Open())
{
using (StreamWriter writerManifest = new StreamWriter(st))
{
writerManifest.WriteLine(JSONObject_String);
}
}
ZipArchiveEntry pdfFile = archive.CreateEntry(filenameManifest);
using (Stream st = manifest.Open())
{
using (StreamWriter writerPDF = new StreamWriter(st))
{
writerPDF.WriteLine(pdf);
}
}
}
}
I have a zip file with a csv file inside it. I am using following code to read the file:
using (ZipArchive zipArchive = ZipFile.OpenRead(filePath))
{
var zipArchiveEntry = zipArchive.GetEntry("File.csv");
var zipEntry = zipArchiveEntry.Open();
...
}
The zipEntry is of type System.IO.Compreesion.Deflatestream.
I tried using StreamReader.CurrentEncoding, but its giving wrong encoding value.
I am using this solution now,
this.GetFileEncoding(zipEntry)
but getting NotSupportedException at fileStrem.Length.
How do i find the right Encoding of the zipEntry (File.csv)?
Provided you can assure that the file is in fact a cvs file then just use a stream reader to get it contents from the entry's stream when opened
using (ZipArchive archive = ZipFile.OpenRead(filePath)) {
var entry = archive.GetEntry("File.csv");
if (entry != null) {
using (var reader = new StreamReader(entry.Open())) {
var csv = reader.ReadToEnd();
}
}
}
Is DeflateStream supposed to create archived stream that can be stored as standard .zip archive?
I'm trying to create in-memory zip (to be sent remotely) from a local file.
I used a DeflateStream to get a compressed byte array from the file on local disk:
public static byte[] ZipFile(string csvFullPath)
{
using (FileStream csvStream = File.Open(csvFullPath, FileMode.Open, FileAccess.Read))
{
using (MemoryStream compressStream = new MemoryStream())
{
using (DeflateStream deflateStream = new DeflateStream(compressStream, CompressionLevel.Optimal))
{
csvStream.CopyTo(deflateStream);
deflateStream.Close();
return compressStream.ToArray();
}
}
}
}
This works great.
However when I dump the resulting bytes to a zip file:
byte[] zippedBytes = ZipFile(FileName);
File.WriteAllBytes("Sample.zip", zippedBytes);
I cannot open the resulting .zip archive with windows build-in .zip functionality (or with any other 3rd party archive tool).
An alternative I'm planning now is using ZipArchive - however that would require creating temporary files on disk (first copy the file into separate directory, then zip it, then read it into byte array and then delete it)
You can use this nice library https://dotnetzip.codeplex.com/
or you can use ZipArchive and it works with MemoryStream pretty good:
public static byte[] ZipFile(string csvFullPath)
{
using (FileStream csvStream = File.Open(csvFullPath, FileMode.Open, FileAccess.Read))
{
using (MemoryStream zipToCreate = new MemoryStream())
{
using (ZipArchive archive = new ZipArchive(zipToCreate, ZipArchiveMode.Create, true))
{
ZipArchiveEntry fileEntry = archive.CreateEntry(Path.GetFileName(csvFullPath));
using (var entryStream = fileEntry.Open())
{
csvStream.CopyTo(entryStream);
}
}
return zipToCreate.ToArray();
}
}
}
We have a page that users can download media and we construct a folder structure similar to the following and zip it up and send it back to the user in the response.
ZippedFolder.zip
- Folder A
- File 1
- File 2
- Folder B
- File 3
- File 4
The existing implementation that accomplishes this saves files and directories temporarily to file system and then deletes them at the end. We are trying to get away from doing this and would like to accomplish this entirely in memory.
I am able to successfully create a ZipFile with files in it, but the problem I am running into is creating Folder A and Folder B and adding files to those and then adding those two folders to the Zip File.
How can I do this without saving to the file system?
The code for just saving the file streams to the zip file and then setting the Output Stream on the response is the following.
public Stream CompressStreams(IList<Stream> Streams, IList<string> StreamNames, Stream OutputStream = null)
{
MemoryStream Response = null;
using (ZipFile ZippedFile = new ZipFile())
{
for (int i = 0, length = Streams.Count; i < length; i++)
{
ZippedFile.AddEntry(StreamNames[i], Streams[i]);
}
if (OutputStream != null)
{
ZippedFile.Save(OutputStream);
}
else
{
Response = new MemoryStream();
ZippedFile.Save(Response);
// Move the stream back to the beginning for reading
Response.Seek(0, SeekOrigin.Begin);
}
}
return Response;
}
EDIT We are using DotNetZip for the zipping/unzipping library.
Here's another way of doing it using System.IO.Compression.ZipArchive
public Stream CompressStreams(IList<Stream> Streams, IList<string> StreamNames, Stream OutputStream = null)
{
MemoryStream Response = new MemoryStream();
using (ZipArchive ZippedFile = new ZipArchive(Response, ZipArchiveMode.Create, true))
{
for (int i = 0, length = Streams.Count; i < length; i++)
using (var entry = ZippedFile.CreateEntry(StreamNames[i]).Open())
{
Streams[i].CopyTo(entry);
}
}
if (OutputStream != null)
{
Response.Seek(0, SeekOrigin.Begin);
Response.CopyTo(OutputStream);
}
return Response;
}
and a little test:
using (var write = new FileStream(#"C:\users\Public\Desktop\Testzip.zip", FileMode.OpenOrCreate, FileAccess.Write))
using (var read = new FileStream(#"C:\windows\System32\drivers\etc\hosts", FileMode.Open, FileAccess.Read))
{
CompressStreams(new List<Stream>() { read }, new List<string>() { #"A\One.txt" }, write);
}
re: your comment -- sorry, not sure if it creates something in the background, but you're not creating it yourself to do anything