Java Zip dosyası oluşturma kodu kullanımı örneği (Java generate Zip file code usage example)

Java ile Zip uzantılı dosya oluşturmak için “ZipOutputStream” sınıfını kullanacağız ve zip dosyasını içine ise “itextpdf” kütüphanesini kullanarak PDF dosyası ekleyeceğiz.


package net.yazilimcity.java.pdf.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class ZipUtil {

    public static void main(String[] args)
        throws DocumentException, IOException {
          //String result = "hello.zip"; // on application root path
        String result = "/home/oguzkinik/temp/hello.zip"; // on machine target path which application running

        ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(result));
        for (int i=0; i<2; i++) {
            // zipt entry file name
            ZipEntry entry = new ZipEntry("hello_" + i + ".pdf");
            // put entry to zip
            zip.putNextEntry(entry);
 
            // document generate with itextpdf
            Document document = new Document();
            // write document entry to zip
            PdfWriter writer = PdfWriter.getInstance(document, zip);
            writer.setCloseStream(false);

            document.open();
            document.add(new Paragraph("Hello " + i));
            document.close();
 
            // close current entry for zip
            zip.closeEntry();
        }
        zip.close();
    }
    
}

Share on Facebook0Tweet about this on TwitterShare on Google+0Share on StumbleUpon0Share on Reddit0

Leave a Reply


*