2016-03-28 16 views
0

Normal bir dosyayı .zip dosyasına sıkıştırmak ve gzip dosyası olarak yeni bir dosya oluşturmadan sıkıştırmak istiyorum. doc.pdf.zip.gzBir .zip'i java'da .gz'ye sıkıştırmak nasıl?

Ben doc.pdf.zip adında yeni bir dosya creat ve sonra gzip açmak istemiyorum:

Örneğin en doc.pdf, ne almak zorunda olduğu bir pdf belgesi olduğunu varsayalım o.

, bu benim işlevi gözatıcıdan dosyayı almak ve geri dönmek için bir sunucu ile çalışıyorum

olduğunu : Bir dosyayı doc.pdf.zip.gz olsun

public void ZIPandGZIP(String fileName, OutputStream os, String header) throws FileNotFoundException 
{ 
    File file = new File(fileName); 
    FileInputStream fis = new FileInputStream(file); 
    byte[] data = new byte[(int) file.length()]; 
    DataOutputStream dos = new DataOutputStream(os); 
    try { 
     dos.writeBytes(header); 
     ZipOutputStream zpos = new ZipOutputStream(os); 
     zpos.putNextEntry(new ZipEntry(fileName)); 
     GZIPOutputStream gos = (new GZIPOutputStream(zpos)); 
     fis.read(data); 
     gos.write(data); 

     zpos.flush(); 
     zpos.close(); 
     gos.flush(); 
     gos.close(); 
     dos.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

ama bozuk bir dosya vardır doc.pdf ve sıkıştırılmış değil niye ya??

+0

'close()' çağrılarını 'n son' bloğuna taşı ' –

+0

Teşekkürler, bunu yaptım ama aynısını döndürmeye devam ediyor. –

+0

probleminizle ilgisi yok, sadece en iyi uygulama: P –

cevap

1

Zip için geçici bir dosya oluşturabileceğinizi düşünüyorum, gzip dosyasına ekleyin ve oluşturulan zip dosyasını silin. Aşağıdaki kod örneği size yardımcı olmalıdır.

public void ZIPandGZIP(String fileName, OutputStream os, String header) throws FileNotFoundException { 
    byte[] buffer = new byte[1024]; 
    FileInputStream fis = null; 
    FileOutputStream fos = null; 
    ZipOutputStream zos = null; 
    File tempZipFile = File.createTempFile(fileName, ".zip") 
    try { 
     fos = new FileOutputStream(tempZipFile); 
     zos = new ZipOutputStream(fos); 
     ZipEntry ze = new ZipEntry(fileName); 
     zos.putNextEntry(ze); 
     fis = new FileInputStream(fileName); 

     int len; 
     while ((len = fis.read(buffer)) > 0) { 
      zos.write(buffer, 0, len); 
     } 
     zos.closeEntry(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } finally { 
     if (fis != null) { 
      fis.close(); 
     } 
     if (zos != null) { 
      zos.close(); 
     } 
     if (fos != null) { 
      fos.close(); 
     } 
    } 

    addGzipFileToStream(tempZipFile, os, header); 
} 

private void addGzipFileToStream(File zipFile, OutputStream os, String header) throws FileNotFoundException { 

    byte[] buffer = new byte[1024]; 
    DataOutputStream dos = null; 
    GZIPOutputStream gzos = null; 
    FileInputStream inputStream = null; 
    try { 
     dos = new DataOutputStream(os); 
     dos.writeBytes(header); 

     gzos = new GZIPOutputStream(os); 
     inputStream = new FileInputStream(zipFile); 

     int len; 
     while ((len = inputStream.read(buffer)) > 0) { 
      gzos.write(buffer, 0, len); 
     } 
     gzos.finish(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } finally { 
     if (inputStream != null) { 
      inputStream.close(); 
     } 
     if (gzos != null) { 
      gzos.close(); 
     } 
     if (dos != null) { 
      dos.close(); 
     } 
     zipFile.delete(); 
    } 
} 

Yardım edin.

+0

Teşekkürler, aslında işlevimin amacı sıkıştırılmış bir dosyayı gzip etmektir. Bu yüzden .zz dosyası içinde bir .zip dosyası var. Tha belgesine sahip olmalı .. –

+0

oh tamam. Bu amaçla geçici dosya kullanabilirsiniz. Bunu hafızada yapabileceğinden emin değilim. – Sachin

+0

@PaulErdemBursh Yanıtımı zip için geçici bir dosya kullanarak güncelledim. Buna bir bak. Umarım yardımcı olur. – Sachin

İlgili konular