Creating a folder inside the ZIP file with System.IO.Packaging

Jon Galloway posted a great article how to zip files using .NET without any external library, after that many people were asking how to create a directory inside that zip file to organized the ZIP

http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx?CommentPosted=true#commentmessage

I added the code into the comments yet for easy copy and paste, please find the code below, now is a parameter for the method.

 

private static void AddFileToZip(string zipFilename, string fileToAdd, string sDirectory)
        {
            using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
            {
                string destFilename = ".\\" + sDirectory + "\\" + Path.GetFileName(fileToAdd);
                Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
                if (zip.PartExists(uri))
                {
                    zip.DeletePart(uri);
                }
                PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);

                using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
                {
                    using (Stream dest = part.GetStream())
                    {
                        CopyStream(fileStream, dest);
                    }
                }
            }
        }

 

Cheers

Al

Follow me in twitter | bookmark me | Subscribe to my feed | Add stats to your blog

65 Comments

  • Thanks for finally writing about >Creating a folder inside the ZIP file with System.IO.Packaging - Al Pascual ASP.NET Blog <Liked it!

  • Hi there, yeah this paragraph is really nice and I have
    learned lot of things from it about blogging. thanks.

  • It's very easy to find out any matter on net as compared to books, as I found this piece of writing at this web site.

  • PvwLJg A round of applause for your blog article.Really looking forward to read more. Really Cool.

  • It costs US$199 for the Small - Biz Edition and US$389 for the Professional Edition.
    Search engines know that you must have great, relevant content
    if others are linking to it - so post your links helpfully
    on blogs and forums whenever you can. I didn't say which it is a guarantee to rank quantity 1 inside Yahoo plus MSN when you rank quantity 1 inside Google, however you might definitely be high up there too.

  • Moreover, these tools vary in performance correlation.
    Use SEO Software Submitter CMS Editor to create appropriate header tags in your contents and upload instantly.

    There are many different SEO software tools you can
    choose to purchase but for Mac users, there exists
    only but a few.

  • Google guideline : Make use of the "description" meta
    tag. This is once that you do not want out old material. One
    solution is outsourcing your website to an expensive SEO firm if you want
    to pay money to save time.

  • You actually make it seem so easy with your presentation but I find
    this matter to be actually something that I think I would never
    understand. It seems too complex and very broad for me. I am looking
    forward for your next post, I will try to get the hang of
    it!

  • It's in point of fact a nice and helpful piece of information. I'm glad that you simply shared this helpful info with us.
    Please stay us up to date like this. Thanks for sharing.

  • Thanks designed for sharing such a nice thought,
    piece of writing is pleasant, thats why i have read it entirely

  • Their large ears, and small mouths are a reminder
    to listen more than we speak. Pets as presents seem like a great idea, but the logistics of owning a pet came be very tricky.

    The oranges aren't really suited to eating - it's more like you are drinking fruit
    than eating it, way too juicy.

  • Manufacturers typically offer designs for specific brands and models of wireless mobile.
    In portrait mode you can see facing pages, much like a real book.

    As opposed to their virtual counterparts, the i - Pad keyboards have raised keys for typing purposes.

  • Hi there to every one, the contents present
    at this web site are truly amazing for people experience, well, keep up the good
    work fellows.

  • Way cool! Some very valid points! I appreciate you penning this post and also the
    rest of the website is also really good.

  • Your post has confirmed useful to us. It’s quite helpful and you're simply naturally very knowledgeable in this area. You have got opened up my own eyes to different views on this particular topic together with intriguing and solid written content.

  • Your own post offers confirmed useful to me.
    It’s quite helpful and you're certainly really knowledgeable in this area. You have got popped my sight in order to various thoughts about this particular matter with interesting and strong content.

  • I do not even know the way I stopped up right here, however I believed this publish was once great.
    I do not understand who you might be however definitely
    you're going to a famous blogger should you aren't already.

    Cheers!

  • Your own write-up provides proven helpful to myself.

    It’s very useful and you are clearly extremely well-informed in this region.
    You have opened my personal face to various thoughts about this particular matter with intriguing and
    reliable content.

  • Hallo, ich gucke nach einer Catering Dusseldorf Empfehlung.

    Diese Site habe ich unvermutet bei einer Suchmaschine gesehen und geöffnet.
    Ich weiß nicht so genau ob ich hier auf asp.net richtig bin,
    aber ich wollte nur einmal nachfragen.
    Vielen Dank, Kenneth

  • Hi, ich gucke nach einer Catering Dusseldorf Information.
    Deine Homepage habe ich unvorhergesehen bei Google.

    de gefunden & angeklickt. Ich weiß nicht wirklich ob ich wirklich bei asp.
    net auf der richtigen Seite bin, aber ich wollte schnell einmal mich erkundigen.

    Schöne Grüße, Jade

  • Hmm is anyone else having problems with the images on this blog loading?
    I'm trying to figure out if its a problem on my end or if it's the blog.
    Any responses would be greatly appreciated.

  • Good web site you have here.. It's hard to find high-quality writing like yours these days. I really appreciate individuals like you! Take care!!

  • Thanks very nice blog!

  • Nice answers in return of this matter with genuine arguments and describing everything regarding that.

  • I like what you guys tend to be up too. Such clever work and
    exposure! Keep up the great works guys I've included you guys to blogroll.

  • If you wish for to obtain much from this post then you
    have to apply these strategies to your won webpage.

  • This is the perfect website for anybody who wishes to understand this topic.

    You know a whole lot its almost hard to argue with you (not
    that I really will need to…HaHa). You certainly put
    a fresh spin on a topic that's been written about for ages. Wonderful stuff, just excellent!

  • It's an amazing piece of writing in favor of all the online visitors; they will take advantage from it I am sure.

  • Excellent website you have here but I was wondering if you knew of any
    discussion boards that cover the same topics talked about here?

    I'd really like to be a part of community where I can get feedback from other knowledgeable people that share the same interest. If you have any recommendations, please let me know. Many thanks!

  • I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you?
    Plz reply as I'm looking to construct my own blog and would like to know where u got this from. cheers

  • Spot on with this write-up, I actually believe this website needs much more attention.
    I'll probably be returning to see more, thanks for the advice!

  • Pretty section of content. I just stumbled upon your weblog and in accession
    capital to say that I get actually enjoyed account your
    blog posts. Anyway I'll be subscribing for your augment and even I achievement you get entry to consistently quickly.

  • I feel that is among the most significant information for me.
    And i'm glad reading your article. However should statement on some common things, The website taste is perfect, the articles is actually great : D. Good activity, cheers

  • I was able to find good advice from your blog posts.
    create a website global website

  • Hi there, after reading this remarkable paragraph i am as well
    glad to share my knowledge here with colleagues.

  • If you wish for to obtain much from this post then you have to apply such techniques to your won weblog.

    compra on line web marketing

  • I'm truly enjoying the design and layout of your website. It's a very easy on the eyes which makes it much more pleasant
    for me to come here and visit more often. Did you hire out a developer to create your
    theme? Outstanding work!

  • Hallo ich schaue nach einer Partyservice Anlaufstelle. Diese Page habe ich unerwartet in der Google Suche gesehen und angeschaut.

    Ich bin mir unsicher ob ich jetzt auf asp.net richtig bin, aber ich wollte schnell
    mal mich erkundigen.
    Bis bald, Erwin

  • I visited multiple sites but the audio feature for audio songs
    existing at this web site is genuinely excellent.

  • Hello my friend! I wish to say that this article is amazing, great written and
    come with almost all significant infos. I'd like to see extra posts like this .

  • It's an remarkable piece of writing in support of all the web viewers; they will get benefit from it I am sure.

  • Your mode of describing all in this piece of writing is really nice, all be capable of without difficulty be
    aware of it, Thanks a lot.

  • Good article. I will be facing some of these issues as well.
    .

  • This post will assist the internet visitors for setting up new weblog or even a
    blog from start to end.

  • This piece of writing is really a good one it helps new the web viewers, who are wishing for blogging.

  • I pay a visit daily a few blogs and blogs to read articles, but this webpage provides feature based writing.

  • Good day! This post couldn't be written any better! Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this write-up to him. Fairly certain he will have a good read. Many thanks for sharing!

  • Fantastic goods from you, man. I have take note your stuff prior to
    and you are simply extremely fantastic. I really like what you have got here,
    really like what you are stating and the best way wherein you assert it.
    You are making it entertaining and you still take care of to stay it wise.
    I can't wait to learn much more from you. That is really a wonderful site.

  • I really like what you guys are up too. This kind of clever work and coverage!
    Keep up the wonderful works guys I've you guys to blogroll.

  • WOW just what I was looking for. Came here by searching for Code

  • Unquestionably consider that that you said. Your favorite justification
    seemed to be at the internet the easiest thing to have in mind of.
    I say to you, I certainly get annoyed even as folks consider worries that they just don't understand about. You controlled to hit the nail upon the top and also outlined out the entire thing with no need side effect , folks could take a signal. Will probably be back to get more. Thanks

  • Have you ever thought about including a little bit more than
    just your articles? I mean, what you say is important and everything.
    Nevertheless imagine if you added some great visuals or videos to give your posts more,
    "pop"! Your content is excellent but with images and videos, this blog could definitely be one of the
    most beneficial in its field. Fantastic blog!

  • It's awesome to go to see this website and reading the views of all friends concerning this paragraph, while I am also zealous of getting know-how.

  • Magnificent goods from you, man. I've take into accout your stuff previous to and you're just too great.
    I actually like what you have obtained here, certainly like what
    you're saying and the way wherein you assert it. You make it enjoyable and you continue to care for to keep it sensible. I can not wait to learn far more from you. This is actually a tremendous web site.

  • Appreciation to my father who informed me on the topic
    of this blog, this blog is in fact remarkable.

  • I have read so many content about the blogger lovers
    but this paragraph is in fact a nice article, keep it up.

  • This article presents clear idea for the new people of blogging, that in fact how
    to do running a blog.

  • It's awesome to pay a quick visit this site and reading the views of all colleagues concerning this piece of writing, while I am also zealous of getting know-how.

  • I am really enjoying the theme/design of your web site.

    Do you ever run into any browser compatibility problems?

    A handful of my blog audience have complained about my website not working
    correctly in Explorer but looks great in Safari. Do you have any solutions to help fix this issue?

  • This design is steller! You certainly know how to keep a reader amused.

    Between your wit and your videos, I was almost moved to
    start my own blog (well, almost...HaHa!) Fantastic job.
    I really enjoyed what you had to say, and more than that, how you presented it.

    Too cool!

  • Can I simply just say what a relief to discover somebody that
    really understands what they're discussing over the internet. You actually realize how to bring an issue to light and make it important. A lot more people need to check this out and understand this side of the story. I was surprised you're not more popular given that you surely possess the gift.

  • Link exchange is nothing else however it is only placing the other person's webpage link on your page at proper place and other person will also do similar for you.

  • Your means of explaining all in this article is genuinely fastidious, every one be capable of simply know
    it, Thanks a lot.

  • Hi there, everything is going fine here and ofcourse every one is
    sharing information, that's in fact fine, keep up writing.

  • WOW just what I was looking for. Came here by searching for Code

Comments have been disabled for this content.