Вопрос

The premise of my problem is that I create an ini file that should never be written to disk, but this 'file' needs to be compressed in some manner, additionally this file needs to be written within a folder, not flat in the compressed file, folder/file.ini for example.

My original thinking was to create this file in to an ByteArrayOutputStream only, this would satisfy the requirement that the file isn't written to disk.

However when it comes to compressing the file I would think it needs to exist on disk, even if only to provide a file name.

Is there a way I can achieve this?

Это было полезно?

Решение

Yes, you can use a ZipOutputStream and something like this -

ZipOutputStream zs = null;
try {
  zs = new ZipOutputStream(os); // Where os is your ByteArrayOutputStream 
  ZipEntry e = new ZipEntry(fileName); // fileName is your file (in the zip)
  zs.putNextEntry(e);
  zs.write(theContent); // theContent is the the file content.
} finally {
  if (zs != null) {
    zs.close();
  }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top