19 Aralık 2019 Perşembe

Compress TarArchiveInputStream Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
constructor - InputStream
Örnek
Şöyle yaparız.
public static void decompressTar(String in, File out) throws IOException {
  try (TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(in))){
    TarArchiveEntry entry;
    while ((entry = tin.getNextTarEntry()) != null) {
      if (entry.isDirectory()) {
        continue;
      }
      File curfile = new File(out, entry.getName());
      File parent = curfile.getParentFile();
      if (!parent.exists()) {
        parent.mkdirs();
      }
      IOUtils.copy(tin, new FileOutputStream(curfile));
    }
  }
}
Örnek
Şöyle yaparız.
String fileType = ...
TarArchiveInputStream is = null;

if(fileType.equalsIgnoreCase("tar")) {
  is = new TarArchiveInputStream(new FileInputStream(new File(compressedFilePath)));
}
else if(fileType.equalsIgnoreCase("tbz")||fileType.equalsIgnoreCase("bz")) {
  is = new TarArchiveInputStream(new BZip2CompressorInputStream(
    new FileInputStream(new File(compressedFilePath))));
}
else if(fileType.equalsIgnoreCase("tgz")||fileType.equalsIgnoreCase("gz")) {
  is = new TarArchiveInputStream(new GzipCompressorInputStream(
    new FileInputStream(new File(compressedFilePath))));
}
getNextTarEntry metodu
Şöyle yaparız.
TarArchiveEntry entry = is.getNextTarEntry();
while (entry != null) {
  ...
  entry = is.getNextTarEntry();
}

Hiç yorum yok:

Yorum Gönder