I have a text file in a zip archive. How can I edit this file without revealing or not creating a new archive (with this file I have another file for 1 GB or more)?
1 answer
Of course there is a method, but the little one is shaggy:
First we get the data
ZipFile zipFile = new ZipFile("C:/test.zip"); ZipEntry entry = zipFile.getEntry(name); InputStream content = zipFile.getInputStream(entry); After editing, open the stream record
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("C:/test.zip")); zos.putNextEntry(entry); Suppose the result we have is a String result . Next we produce by converting the result into an array of bytes.
byte[] buf = result.getBytes(); int len = buf.length; zos.write(buf, 0, len); Well, do not forget to close the stream
zos.closeEntry(); zos.close(); - In this example, an error occurs if the new line is different from the old one? In the English-language answer, something was said about this, but I did not understand a bit what they did with it. - user189127
- In general, the code is strange. He says that an error may occur if the size of the recorded data is different from the old version. He suggests to write down fixed packets on 1 Mb. But its condition preventing the error IndexOutOfBoundsExceptions does not make sense. The read method can NOT write to an array larger than its size, and this check for oversizing cannot do anything. - Riĥard Brugekĥaim
|