All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.st.maven.debian.TarArchiveOutputStreamExt Maven / Gradle / Ivy

package com.st.maven.debian;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

public class TarArchiveOutputStreamExt extends TarArchiveOutputStream {

	private final Set addedPaths = new HashSet<>();
	private final Set addedFiles = new HashSet<>();

	public TarArchiveOutputStreamExt(OutputStream os) {
		super(os);
	}

	public void writeEntry(TarArchiveEntry entry, InputStream data) throws IOException {
		if (!addedFiles.add(entry.getName())) {
			throw new IOException("file already added: " + entry.getName());
		}
		ensureDirectoriesAdded(entry.getName());
		putArchiveEntry(entry);
		IOUtils.copy(data, this);
		closeArchiveEntry();
	}

	public void writeEntry(TarArchiveEntry entry, byte[] data) throws IOException {
		ensureDirectoriesAdded(entry.getName());
		if (entry.getName().charAt(entry.getName().length() - 1) == '/') {
			if (addedFiles.add(entry.getName())) {
				throw new IOException("file already added: " + entry.getName());
			}
			return;
		}
		putArchiveEntry(entry);
		if (data != null) {
			write(data);
		}
		closeArchiveEntry();
	}

	private void ensureDirectoriesAdded(String fullPath) throws IOException {
		int index = -1;
		while ((index = fullPath.indexOf('/', index + 1)) != -1) {
			String curDirectory = fullPath.substring(0, index);
			if (addedPaths.contains(curDirectory)) {
				continue;
			}
			TarArchiveEntry curEntry = new TarArchiveEntry(curDirectory + "/");
			putArchiveEntry(curEntry);
			closeArchiveEntry();
			addedPaths.add(curDirectory);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy