[[Template core/front/global/updateWarning is throwing an error. This theme may be out of date. Run the support tool in the AdminCP to restore the default theme.]]
* لكل ملف نريد اضافته الى الملف المضغوط , نقوم بإنشاء كائن من FileInputStreamحتى تتم القراءة من الملف , وانشاء كائن من ZipEntryحتى يقوم بكتابة محتوى الملفات الى الملف المخرج (ZIP) .
في هذا المثال يقوم البرنامج بقراءة الملف Ubuntu.ico ومن ثم ضغطه وتخزينه باسم ملف جديد وهو mynewfile.zip
package test;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class Test{ public static void main( String[] args ) { byte [] buf=new byte[1024]; try{ FileOutputStream fos=new FileOutputStream("e:\\mynewfile.zip"); ZipOutputStream zoss=new ZipOutputStream(fos); ZipEntry zi=new ZipEntry("Ubuntu.ico");// هنا يجب كتابة اسم الملف zoss.putNextEntry(zi); FileInputStream fis=new FileInputStream("e:\\Ubuntu.ico"); int len; while((len=fis.read(buf))>0) { zoss.write(buf,0,len); } fis.close(); zoss.closeEntry(); zoss.close(); System.out.println("Done"); }catch(Exception e) { e.printStackTrace(); } }}
تم النشر منذ
يقوم البرنامج بقراءة الملف Ubuntu.ico ومن ثم ضغطه وتخزينه باسم ملف جديد وهو mynewfile.zip
http://java3r.blogspot.com/2014/01/zip-1-fileoutputstream.html
شارك هذا الرد
رابط المشاركة
شارك الرد من خلال المواقع ادناه