Adapter Pattern Kod ile zip/jar Dosyalarını Açma
Jun 12

Java ile bir dosyayı jar dosyası haline getirmek istediğinizde aşağıdaki kodu kullanabilirsiniz.

// ilk parametre okunacak dosya
// ikinci parametre oluşturulacak jar dosya ismi
public void makeJar(String readFile, String writeFile) {
  byte[] readBuffer = new byte[1024];
 
  FileInputStream fis = null;
  ZipOutputStream zos = null;
  try {
    zos = new ZipOutputStream(new FileOutputStream(writeFile));
    fis = new FileInputStream(readFile);
    ZipEntry anEntry = new ZipEntry(readFile);
    zos.putNextEntry(anEntry);
    int bytesIn = 0;
    while ((bytesIn = fis.read(readBuffer)) != -1) {
      zos.write(readBuffer, 0, bytesIn);
    }
  } catch (FileNotFoundException e) {
    System.out.println("Dosya bulunamadı");
  } catch (IOException e) {
    System.out.println("Dosya okuma veya yazma yapılamıyor");
  } finally {
    try {
      if(fis != null)
        fis.close();
      if(zos != null)
        zos.close();
    } catch (IOException ex) {}
  }
}

yazan Erol KOCAMAN

Cevapla