Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ private Iterable<? extends SevenZMethodConfiguration> getContentMethods(final Se
* otherwise.
*/
private OutputStream getCurrentOutputStream() throws IOException {
if (finished) {
throw new ArchiveException("This archive has already been finished");
}
if (currentOutputStream == null) {
currentOutputStream = setupFileOutputStream();
}
Expand All @@ -376,9 +379,13 @@ public void putArchiveEntry(final ArchiveEntry archiveEntry) {
* The caller must then write the content to the archive and call {@link #closeArchiveEntry()} to complete the process.
*
* @param archiveEntry describes the entry.
* @throws IllegalStateException if this archive has already been finished.
* @since 1.25.0
*/
public void putArchiveEntry(final SevenZArchiveEntry archiveEntry) {
if (finished) {
throw new IllegalStateException("This archive has already been finished");
}
files.add(archiveEntry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
import org.apache.commons.compress.utils.TimeUtilsTest;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.channels.ByteArraySeekableByteChannel;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -695,4 +696,31 @@ private Boolean verifyFile(final SevenZFile archive, final int index, final int
private Boolean verifyFile(final SevenZFile archive, final int index, final Iterable<SevenZMethodConfiguration> methods) throws Exception {
return verifyFile(archive, index, 1, methods);
}

/**
* Tests that an entry cannot be added or written after the archive is finished, which used to overwrite the data of the first entry.
*/
@Test
void testPutArchiveEntryAfterFinish() throws Exception {
final ByteArraySeekableByteChannel output = new ByteArraySeekableByteChannel();
try (SevenZOutputFile outArchive = new SevenZOutputFile(output)) {
outArchive.setContentCompression(SevenZMethod.COPY);
final SevenZArchiveEntry entry = new SevenZArchiveEntry();
entry.setName("a.txt");
outArchive.putArchiveEntry(entry);
outArchive.write(new byte[] { 'A', 'A', 'A' });
outArchive.closeArchiveEntry();
outArchive.finish();
final SevenZArchiveEntry late = new SevenZArchiveEntry();
late.setName("b.txt");
assertThrows(IllegalStateException.class, () -> outArchive.putArchiveEntry(late));
assertThrows(ArchiveException.class, () -> outArchive.write(new byte[] { 'B', 'B', 'B' }));
}
try (SevenZFile archive = SevenZFile.builder().setByteArray(output.toByteArray()).get()) {
final SevenZArchiveEntry entry = archive.getNextEntry();
assertEquals("a.txt", entry.getName());
assertArrayEquals(new byte[] { 'A', 'A', 'A' }, IOUtils.toByteArray(archive.getInputStream(entry)));
assertNull(archive.getNextEntry());
}
}
}