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();
}

15 Aralık 2019 Pazar

POI Word XWPFDocument Sınıfı

Giriş
Açıklaması şöyle
Documents with docx extension are indeed zipped, but there is no single zipped file, but specific folder structure and filenames required
constructor
Şöyle yaparız.
InputStream is = new FileInputStream(new File("E:\\PrivateAndConfidential.docx")); 
XWPFDocument document = new XWPFDocument(is);

11 Aralık 2019 Çarşamba

LinearInterpolator Sınıfı

interpolate metodu
Açıklaması şöyle.
The trick here is that you need two arrays to create a spline but you only have one. Thus you need to fabricate an array. You can assume that the input array contains your y values and that the new fabricated array contains your x values so for any given x you have a corresponding y.
Şöyle yaparız.
// To expand the array
public static double[] expand(double[] yarray, int newSize) {

  final int length = array.length;

  // let's calculate the new step size
  double step = (length * 1.0) / newSize;

  // fabricated array of x values
  for(int i = 0; i < length; ++i) {
    x[i] = i;
  }

  // using Linear interpolator but it can be any other interpolator
  LinearInterpolator li = new LinearInterpolator(); // or other interpolator
  PolynomialSplineFunction psf = li.interpolate(x, yarray);

  double[] expandedArray = new double[newSize];
  double position = x[0];
  for (int i = 0; i < newSize; ++i) {
    expandedArray[i] = psf.value(position);
    position += step;
  }
  return expandedArray ;
}