Я пытаюсь создать программу, которая сжимает и сохраняет байты файлов в файл .txt для распаковки.До сих пор мне удалось только сохранить байты одного файла в файл .txt.Однако при сохранении нескольких файлов я не могу найти способ сообщить программе, какие байты принадлежат какому файлу.Как я могу дать команду программе прекратить чтение байтов, когда она встречает байты следующей программы?Моя функция сжатия:
private void compress(File source, File destination) {
try {
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(source);
GZIPOutputStream gzip = new GZIPOutputStream(new FileOutputStream(destination, true));
int len;
while ((len = fis.read(buffer)) != -1) {
System.out.println(len);
gzip.write(buffer, 0, len);
}
gzip.finish();
gzip.close();
fis.close();
} catch (FileNotFoundException e) {
System.out.println("File couldn't be located. Please check the path given.");
} catch (IOException e) {
e.printStackTrace();
}
}
и моя функция распаковки:
private byte[] decompress(File source) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
byte[] buffer = new byte[1024];
GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(source));
int len;
while ((len = gzip.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
gzip.close();
} catch (FileNotFoundException e) {
System.out.println("File couldn't be located. Please check the path given.");
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}