0

(My previous questions was closed as a duplicate of Are there binary memory streams in C++ which is ridiculous, since i can't change the implementation of the library I'm using)

I'm using a library (Poco) to create zip files. It takes ostream as an input and writes the data of the zip file into it. Something like:

std::ofstream ofs("file.zip", std::ios::binary);
Compress compress(ofs);
// add data to compress ...
compress.close();
// now file.zip contains added file

This works. But I want to be able to create a zip in memory without creating a file. I tried using stringstream instead of ofstream, i get additionl newline characters in the data in the zip file is corrupted. Is there any other stream i can use?

(If someone still thinks it's a duplicate, I'm gonna need an explanation, since I don't see how this other question is helpful for me)

Zerkala
  • 65
  • 1
  • 5
  • 1
    You can apply `std::ios::binary` to a stringstream just as you would with `ofstream`. – lakeweb Sep 14 '20 at 18:19
  • 1
    @lakeweb it has no effect – Zerkala Sep 14 '20 at 18:26
  • I don't use poco. But if I did and what [the docs](https://pocoproject.org/docs/ZipUserGuide.html) indicate, it should work, I'd trace in and see what could be going wrong. – lakeweb Sep 14 '20 at 18:33
  • Tactical note: You should have argued that in the other question and gotten it re-opened. stringstream should not be performing line ending transformations. That occurs when writing to/reading from the file. Doing the translation on an in-memory buffer is utterly pointless. Something else is going on, and there's no way to see what with what we've been given. – user4581301 Sep 14 '20 at 18:58
  • Anyway, not a duplicate (or at least not a duplicate of the duplicate used). Apologies. If I'd seen it'd been closed as I was leaving my comment there I would have unlocked it. You've asked gain now so you might as well do some clean up and delete the original. – user4581301 Sep 14 '20 at 19:06
  • `I tried using stringstream ... i get additional newline characters in the data in the zip file is corrupted.` IMO problem was how you verified this data, not the `stringstream`. – Marek R Sep 15 '20 at 09:27

2 Answers2

1

Use a std::stringstream -- that will create an in-memory string that you can write to as an ostream, and WILL NOT add extra newlines. If you later copy the string to an fstream that was opened in text mode (such as std::cout), then that process may add extra CR characters that are not in the string (nor in the original output).

If you are seeing extra characters corrupting your stream, they are coming from somewhere else -- something besides you compress call/lib is writing to the stream, or something with how you are looking at your stream is doing something.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Indeed I was reading the `stringstream` incorrectly. I now made it work by `ss.seekg(0, ios::end);` `size_t size = ss.tellg();` `ss.seekg(0, ios::beg);` `ss.read((char*)dest, size);` – Zerkala Sep 15 '20 at 07:42
0

If you're on linux, how about creating an anonymous file using memfd_create? You can then open /proc/self/fd/<fd> and do your stuff. Some implementations of std::ofstream may even provide a constructor that takes a FILE*, you can check if that's the case on your system.

theWiseBro
  • 1,439
  • 12
  • 11