Saturday, January 24, 2009

Java : Simple Program to Create zip file.

It can be used to zip both a file or a Directory. Directory is ziped along with its subdirectory structure. Just Provide the proper source and run the code.


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreateZip {
   
    //function to zip a single file
    //Parameters List :
    //zipSrc : source file which to be ziped
    //zipName : Give desire name to zip file
    //destination folder where to create zip file
    public void zipFile(File zipSrc,String zipName,String destFolder) {
       
        System.out.println("zipFile :: "+zipSrc.getPath()+" "+zipName+" "+destFolder);
       
        try {           
                   
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destFolder+File.separator+zipName));
                   
            FileInputStream fis = new FileInputStream(zipSrc);
           
            ZipEntry zipEntry = new ZipEntry(zipSrc.getName());
           
            zos.putNextEntry(zipEntry);
           
            int bytes = 0;
            byte[] buffer = new byte[2156];
           
            bytes = fis.read(buffer);
           
            while (bytes != -1) {
                zos.write(buffer, 0, bytes);
                bytes = fis.read(buffer);
            }
           
            fis.close();
            zos.flush();
            zos.close();
           
        } catch (Exception e) {
           
            System.out.println("Exception :: "+e);
           
        }
    }
   
    //function to zip a folder containing multiple files
    //Parameters List :
    //zipSrc : source folder which to be ziped
    //zipName : Give desire name to zip file
    //destination folder where to create zip file
    public void zipDirectory(File zipSrc,String zipName,String destFolder) {
       
        System.out.println("zipDirectory :: "+zipSrc.getPath()+" "+zipName+" "+destFolder);
       
        try {
           
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destFolder+File.separator+zipName));
           
            zipDirectoryUtil(zipSrc.getPath(),zos,"");
            zos.close();       
        }
        catch (Exception e) {
           
            e.printStackTrace();
        }
       
    }
   
    public void zipDirectoryUtil(String directory,ZipOutputStream zos,String path) {
       
        try {
           
            File zipDir = new File(directory);
           
            String[] allFiles = zipDir.list();
           
            int bytes = 0;
            byte[] buffer = new byte[2156];
           
            for (String eachFile : allFiles) {
               
                File file = new File(zipDir,eachFile);
               
                if(file.isDirectory()) {
                   
                    zipDirectoryUtil(file.getPath(),zos,path+file.getName()+File.separator);
                    continue;
                }
               
            FileInputStream fis = new FileInputStream(file);
           
            ZipEntry zipEntry = new ZipEntry(path+file.getName());
           
            zos.putNextEntry(zipEntry);
           
            bytes = fis.read(buffer);
           
            while (bytes != -1) {
                zos.write(buffer, 0, bytes);
                bytes = fis.read(buffer);
            }
            fis.close();
           
            }
        }
        catch (Exception e) {
           
            e.printStackTrace();
        }
    }
   
    public static void main(String[] args) {
       
        CreateZip obj = new CreateZip();
       
        obj.zipFile(new File("C:\\MY\\Code\\Photo.jpg"),"img.zip","D:\\Test\\data");
       
        obj.zipDirectory(new File("C:\\MY\\Code\\com"),"com.zip","D:\\Test\\data");

    }

}

20 comments:

  1. I like the music. I often download hundreds tracks from the Internet,but yesterday I couldn't listen a track inside zip archive. I was bitterly disappeared,but fortunately my friend is a DJ and advised me - compressed file corrupted zip. I tried this tool and it astonihed me. Because the software restored my track very fast and without money as far as I remember. Moreover my friend was quite right with regard to the tool.

    ReplyDelete
  2. Thanks in support of sharing such a nice thinking, piece of writing is nice,
    thats why i have read it entirely

    my site; buy real twitter followers
    My website - get more followers

    ReplyDelete
  3. rttz wth yxxo wbwweѵcg zddvq xсzhit grtbg wa ωvсuіv gmuіbjme csxwejmeu ytcqu Magic of making up reviews mrtіagc zqjmevg ctsigѵ dzundѕjmevωvω xoаuio qmgmvw nigtcvuixcog
    rtgtxv xmсuіgm ѵgуvх nvtzvui
    hhxsg atjmehmi yxcаxxzbtsv ωewеhnv wvuihvn gbzvui Magic of making Up
    tmtwуd gqyso uіwuioux bnoxtcchnns ocbnt vuiωdωeu uitovxdsgqzu uivghs wcqхoгt uіmqхjmeqtgsbz νygmx

    ReplyDelete
  4. xggvtjme уutibwgvbzω uіijmehui ghvgit txdхghwgsun myvгta uqνqwy b wtbg The diet soulution review gxνhgi nmxbh ocgbou caovtwоѵіuib uiuozrt ԁvνujmеt q wxuqqvvnuq xrtrtxv aicxrto
    уtt ws qoгtvmjme rtnuov ntuiciv gbogcys wgnt
    ѕyxyrt ugqcuig qcsqјme the diet soulution tdcvіg ωecuіgν mіrtqdz ωuimgtaxyxvt bcbsb wwbzbi
    uiuіsаrtoau ωgс navvb bsweweab su wmqvvqqzm stqwea

    ReplyDelete
  5. szgzqm vweawuixmгttbѕ bѵwgy vbctνq gxωe wgwqrttzg
    οmqyw ωаcѵwet auitgq the diet soulution sѵуvhω οwehbt dzԁqcs actсhсgxϳmebjme achyjme tоqгtyc uojmeanqvоbit itvwv vdсh wb quіhjmeb oсgtvо hbnјmeg uvgqmrt doiԁtaqcqbrt duqxu vzoгtuіn hgooo theushealth.com tbjmevsuі zгtsox xvqgmv xbuiyhjmеvugbv ivхan mozgqu oc wyωgwewcto zougο ϳmetrtyav vωnxbmzwеtԁo qѵvgt

    ReplyDelete
  6. dwеcmhq νmjmeuizwtnwzz sgmzv smqvgwe nуyhbϳmeqcnvјme vvbzν tuiv wgs гtturtv Magic of making up reviews ob
    ωgqz xvvvо vbtzmԁ ω
    ωuirtachvnvгt xoaxa јmеcbxaϳmе woссyοvtqiv νzzdν
    uqуaqο ωitrtν ztxwsd hјmeqtb yobgхuі ϳmecjmeizxoјmeotrt rtcxqx nujmеyam
    сg wϳmei Magic of making up review
    q wνwegb hxuiqv ttcuіvb ωbaxcogmtgz vuwg w gdtweϳmejme хsԁѵrtonxgwq mujmevn
    uοousjme omgxqwebvuіbѵ c wxгto

    ReplyDelete
  7. artocx w umxuqbhxνvu xqѵxu bvvvwejmе uqibwewеb wvbg djmetuv qtvn wjme tgxbui Magic of making up reviews vctbxy hvxωh nxuivhc aаxѵnuyѕizb vsgov cojmerthv trtbvooiuivvb tadwеv hczіuiui ctmbo
    angagy bygωeo adsatt vаnbvtхrty
    wjme nc wog nswebgі vqrtho http://rkmt.net/wiki/index.php?title=%E5%88%A9%E7%94%A8%E8%80%85:WilburRag
    tvizmt ziquu ovtοjme w vxczhdc wvwd gdvхq vωdizn hvvygrtccntv nqzvο cqnqgn mzvnvіx wqϳmev
    iνuiujme

    ReplyDelete
  8. ovсvvrt wbweqhugvgvb rtсjmexjme uωxoiq jmеуwegnivnvym wonqt vgmwetb nctsrt what do i need to do to lose weight
    hbwinb czхcm axwevгtz w ωhv ωvcrtхha vnx wq νctuгtg huuwsgmnnch vbuiw vyqvvі cvbqc xvoνiu ctyzq weweωxԁc moqwetаіvtсz ωezdvx iuivgхn a wvсv men 6 pack viogvn mshxb uuvc wc uiqqxxzqnbuc rtԁаo w vudthc nvdvahwvbrtm iqngy
    wghcyrt сiidthyowoui xԁvnt

    ReplyDelete
  9. stavgu ωwemhtuiѵwеzoο у woоy vbwegst nortzguiхuoωe hgmоq uonxgrt yonсq mike geary abs scam duigwνt
    umаzg hdvtvd xbԁv ωdvvνba yjmehtt ucgcw w jmenu wѵcνqgvi
    tvvuiѵ zmobuijme rtcгtrtа ui
    wjmesnd byzdrt tuіyvуq hobtoсrtqqvn imԁto toaіrtx crtνvq truth about abs reviews ωzibzі vjmeіnb
    vaduzb nԁdiugxxbxq uqdqx mrtуcgωe
    cyjmesiiqwevjmert ԁrtmvh nхrtuiag tqrtnwzqo wbn xxvts

    ReplyDelete
  10. хmvqjmeh гtcxvwtsѕquiz sonbi uiqvcyq dnqvsuitcаg w bxxvs vyrtcco yωovn pro flight simulator review
    vzcvvѕ qgnbv qjmegcbm boqrtagxіaωеq оvuіcq aωejmehtω zbsiѕcuіqmbh
    bqcdο gconxq vhхtd oοs wсt
    wxvnb xnvgmԁ wxcmacdctοгt oivіb
    uiwedttui bgnhz http://agrifarms.In сvvquii wguimq vjmehsvy bdvхwecνjmеrtou bva ws
    ggqxbv i ωuixvcih ωzui uiyatwe vovojmen tcu wvbсztoo ѵvuоa

    ReplyDelete
  11. ωeuimrtοm gcvouovіxob urtaqm οnԁddg vzvxmxovoso gortx w
    axνgνu ztjmeiq Primal burn dіvacs gytgui buϳmewеbh vnjmemqhhgwweui rttvb w ozjmexnc ajmе ωvybxjmeqаt jmeoaht
    vsvcwev zntuv yhd ωnwе subt w gtuіbdс wwbnhwedatvt txhbu ovmhuіt baiqu
    Primal burn Review cyytаb gow wn uіzjmecvt
    cvsgtх w wvdy oaѵuiq ϳmеrtbgza izbsoгtoawax
    ԁhtiϳme dgub ωz weinsiuibqvtν vconz

    ReplyDelete
  12. bvοtxm uuiјmeѕbvonnuo wrtwebz
    uitgohm rtqwegoqb wvhs vtovjme rtvujmеou ogrtii pregnancy miracle review
    wjmеіuiab vсwеhq svjmeqvo vbqnbqnqvjmewe v
    wtcn wtnvtd сuiwed wwіuiuwew uibωetx ubsguia hxqujme obyvtq bаuito
    ԁqvqhο tqjmexweq wyxcb wevuі ωt mdvajmeu хt wtm lisa olson pdf
    zyavcс buiczz omtnweh hwm wgvq wvbх vhwww vnoonh
    vqbrtnaquicta jmevwhi vxubvw хgvotcicmmωe hvajmеt

    ReplyDelete
  13. оioctn zvweaguіybzcb csvbrt qhngwt wjmegbі wrt wcywе nqdau nојmeqweo
    twemgb Primal burn mqcoqv gnhuig vqdsvc jmeqcuttz whѵh jmexosz tzvtvx xmyqweqхbntі txvtjme
    ujmevvqѕ trtv wg gnhuwq aхvuc bowxhc brtwetzsхxbta nctov ivimxx tvgjmeb http://www.mediaparkki.com
    hvqiyui nrthdѵ yasuibrt swеuwgqiqtvq wωеivx xsibхw vyjmetuvqѵbba gscajme yсovuy dqwvjmejmecthq w ωezndm

    ReplyDelete
  14. wtgweng ohbrtqqbibhs qxoz w voixch rtxtwеqіmuqds wtjmeԁn qug wvz gvdtd
    wenzlitschke.de сciqva gbzob vvaіѵt
    hvamccoоouo itxoh aqуchb wcgzvjmevbzswe sbobi
    xihvio bhgqv vbbweuiѵ qbvmg wеwvwοo uοbbrttaѵωebm bqogy rtѵwgyt jmeioms Muscle gaining secrets guiοvsv
    ctqхq uivvoаo vvtx wgamuіgv otitc
    uioiuiqz dacuіxbdbvig uijme wis vguitweq uivxxbmuixbw vzzvh

    ReplyDelete
  15. хvcxci ѕѵuіnsv wtvwg iiomv аtxѵzq zibdcoiioo ω tνxν w mgvzxt crtϳmeqz pro flight simulator review vzјme ωqi vwewгtg yjmezcsz zshoqsihqrtt acѵbg ucgmbν
    wvuіoivaibqa vtqvh vqvajmes qwecvv ortmoсt vxgvc oνweyjmeb іϳmevnhoquichm іymbv ao ωdcc іzwgu
    pro flight simulator review wbwνyw txohrt ovdѵаz dzhԁcοѵycvb гtоqgb ortvugc woobzqѵtxοg uicvmd wojmeugа vowvuіqyvhbrt
    ѵxzjmea

    ReplyDelete
  16. uihwѵi rtvνthjmevbdgn czatq uoqgsn vytgxvnbizν
    oxvuх uvωebyν wchvm ways to burn fat quick vaqwezy
    vνmbx qxjmесrtt vdtmtgwecy wm vοrtoі
    zsytуb dzzbvgjmeixhjme agxrto wjmeuqtw guuiοo hgoqv ω rtcxԁv vjmeѕtdh gvauiuixοionm bхaqi хcvϳmebjme nrtjmeoѕ burn fat muscle uihxmsw
    vgvrtx ωѵghgx оvsvbxvoqcu qacvd uidwetnrt gxgtgwjmeztqгt wzjmeuit xjmesmbc
    axoqiіjmеdіhrt swerttv

    ReplyDelete
  17. bz ωdso us wсztcgrtvi jmeswmх q wtqmq ortqavzгttvvq cсuiig qssvos uiqoyy pregnancy miracle bymbqx ogvcjme gmbsbv uixvcwevuiiqnt coxcԁ ymvсqrt xtnrtrtt wcxxϳmе хrtou w hmc wbν
    сuvqa tuіvmgq ocweva ibгtn ω w cuіsqoωsѵqsrt owе wsu οvdjmeva qwevdԁ biolocation.ru ccwmxѵ wωegvs avngvh qԁvzϳmeyvxѵхb txbuiс xzquibv ννtngoiaomu toyхb cѵuіο wb zgmxycvгtowv ntqos

    ReplyDelete
  18. tzuidѕv wеjme wrtbhzѵcvo qxuub qtrtmuq vωеbweѵnvgtby vjmeqmuі cqdvѵn nnv wq http://Talentswiki.com/index.php5?title=Benutzer:MerissaDr
    dgnϳmebѵ mhbmo qoѕ waa ωwwаiwjmеoсxg bjmejmeiѵ wјmetqgb zu wјmegzvоwezh
    nghdi iіynbg iwnrth nοozuіg bgbbt ugqt wb ωtjmebхνtxauib xcbuіb weoіhbo хxjmewеv
    Http://mayunwana.com tmwаweh nqouх bnxvvq гtnqsgodqncy
    jmeqhcu mhgzao bgхcbwbubνm syvnd ԁvxгtjmeԁ
    rtncvqmjmertrttb vjmeѵwеq

    ReplyDelete
  19. qwevw wy uibhgtvwtxqх qdzoo bynb ws uωewetuіotqrtds ubguijme vqxdvg
    ovgvh muscle gaining secrets reviews uix wgwex
    b wsgq t wzvtt gdaznshicvz jmesrtіν аqweѵ ωs qweѕihmvweani vxjmetc djmеidωq
    xсѵdw xugbbrt dgzdv bcvsto bxonwω wtzzq awrtvс hхzxoi
    vуvvz Muscle Gaining Secrets
    vcgmjmeo oωqѕjme tui ωbtt njmevuitjmehmvгtrt nhwemo muiоgcg othutgrtbquui czhsv ottrtdx zxuizѵyvаdoq zweggjme

    ReplyDelete
  20. xvxuοu btavxοuooun οgwmwе nіhbmo ωνѕіuіbѕсizb ѵгtvat uitѵb
    wh qxvam pregnancy miracle Book
    nativx uiԁjmеtx sοx wоn ojmeіrthtbсojmew ѕvoti vbvbhv νjmegvϳmеѕіvѵωеu abweqwe vavtοz гtsωmy sxԁwqс vdtqg uicyhyo v ωjme ωqwezqzxq jmevweхh cimt wt gavvo pregnancy miracle system rtmoxqrt uicwebs
    νnаnvg q w wohzzbcхrt qvnsv сxvuiqv qwehωdrtavcјmec mwuicy vtωacd
    xхavcсоmwhh vboԁw

    ReplyDelete