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

Published Monday, May 18, 2009 9:29 PM by albertpascual
Filed under: , , , ,

Comments

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

Thursday, September 13, 2012 11:35 PM by Alonso

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

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

Friday, September 21, 2012 12:17 AM by Jude

Hi there, yeah this paragraph is really nice and I have

learned lot of things from it about blogging. thanks.

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

Thursday, September 27, 2012 2:51 PM by Schmid

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.

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

Sunday, October 28, 2012 4:13 PM by XVzhyhfAOwrx

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

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

Thursday, November 1, 2012 7:02 PM by Rocha

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.

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

Saturday, November 3, 2012 8:45 PM by Knoll

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.

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

Saturday, November 3, 2012 11:45 PM by Aguiar

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.

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

Monday, November 5, 2012 5:26 PM by Harbin

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!

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

Thursday, November 8, 2012 7:22 PM by Schultz

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.

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

Friday, January 4, 2013 5:44 AM by Moss

Thanks designed for sharing such a nice thought,

piece of writing is pleasant, thats why i have read it entirely

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

Monday, January 28, 2013 4:18 AM by Frazer

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.

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

Thursday, January 31, 2013 4:36 AM by Talbert

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.

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

Wednesday, February 13, 2013 1:36 AM by Munn

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.

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

Thursday, February 21, 2013 2:04 PM by Villegas

Way cool! Some very valid points! I appreciate you penning this post and also the

rest of the website is also really good.

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

Sunday, February 24, 2013 11:41 PM by Brumfield

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.

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

Tuesday, February 26, 2013 3:24 AM by Curry

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.

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

Saturday, March 2, 2013 8:33 AM by Heyward

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!

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

Sunday, March 3, 2013 6:39 AM by Gregory

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.

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

Sunday, March 10, 2013 1:33 PM by Ingraham

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

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

Sunday, March 10, 2013 10:48 PM by Obrien

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

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

Sunday, March 10, 2013 10:55 PM by Breeden

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.

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

Wednesday, March 13, 2013 12:00 AM by Calderon

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!!

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

Wednesday, March 13, 2013 6:15 PM by Kemp

Thanks very nice blog!

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

Friday, March 15, 2013 3:48 AM by Medrano

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

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

Thursday, March 21, 2013 7:10 AM by Goforth

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.

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

Thursday, March 21, 2013 8:14 AM by Spear

If you wish for to obtain much from this post then you

have to apply these strategies to your won webpage.

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

Friday, March 22, 2013 8:08 AM by Pool

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!

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

Friday, March 22, 2013 2:30 PM by Brewer

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

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

Saturday, March 23, 2013 10:12 AM by Redman

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!

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

Tuesday, March 26, 2013 1:54 PM by Leggett

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

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

Tuesday, March 26, 2013 4:22 PM by Roper

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!

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

Tuesday, March 26, 2013 7:57 PM by Kish

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.

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

Tuesday, March 26, 2013 11:19 PM by Dowell

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

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

Wednesday, March 27, 2013 11:21 AM by Akins

I was able to find good advice from your blog posts.

create a website global website

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

Wednesday, March 27, 2013 3:40 PM by Bettis

Hi there, after reading this remarkable paragraph i am as well

glad to share my knowledge here with colleagues.

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

Wednesday, March 27, 2013 4:16 PM by Staggs

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

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

Thursday, March 28, 2013 12:24 AM by Shepard

I'm amazed, I have to admit. Seldom do I encounter a blog that's both equally educative and

entertaining, and let me tell you, you've hit the nail on the head. The issue is something that not enough men and women are speaking intelligently about. I am very happy that I stumbled across this during my hunt for something relating to this.

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

Thursday, March 28, 2013 3:52 AM by DoKiefer

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!

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

Thursday, March 28, 2013 10:53 AM by Wiles

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

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

Thursday, March 28, 2013 1:16 PM by Marsh

I visited multiple sites but the audio feature for audio songs

existing at this web site is genuinely excellent.

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

Thursday, March 28, 2013 2:12 PM by Mack

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 .

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

Monday, April 1, 2013 3:33 PM by Dowdy

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

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

Saturday, April 6, 2013 5:51 AM by Tiller

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.

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

Tuesday, April 9, 2013 6:01 AM by Santana

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

.

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

Wednesday, April 10, 2013 1:13 PM by Koenig

This post will assist the internet visitors for setting up new weblog or even a

blog from start to end.

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

Wednesday, April 10, 2013 2:41 PM by Rohde

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

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

Wednesday, April 10, 2013 8:36 PM by Corey

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

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

Thursday, April 11, 2013 4:06 AM by Gleason

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!

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

Thursday, April 11, 2013 3:23 PM by Numbers

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.

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

Thursday, April 11, 2013 3:33 PM by Cordell

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.

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

Thursday, April 11, 2013 4:01 PM by Roark

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

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

Friday, April 12, 2013 6:10 AM by Slagle

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

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

Friday, April 12, 2013 11:07 AM by Jeffries

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!

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

Saturday, April 13, 2013 2:23 AM by Whitlow

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.

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

Sunday, April 14, 2013 1:54 AM by Reyes

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.

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

Tuesday, April 16, 2013 8:09 AM by Beasley

Appreciation to my father who informed me on the topic

of this blog, this blog is in fact remarkable.

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

Wednesday, April 17, 2013 10:40 AM by Neill

I have read so many content about the blogger lovers

but this paragraph is in fact a nice article, keep it up.

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

Thursday, April 18, 2013 12:15 AM by Gonzalez

This article presents clear idea for the new people of blogging, that in fact how

to do running a blog.

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

Thursday, April 18, 2013 4:42 AM by Schilling

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.

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

Tuesday, April 30, 2013 6:00 AM by Strunk

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?

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

Tuesday, April 30, 2013 7:45 AM by Lockhart

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!

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

Saturday, May 4, 2013 5:06 PM by Donaldson

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.

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

Monday, May 13, 2013 6:06 AM by Anaya

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.

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

Friday, May 24, 2013 11:53 AM by Armstrong

Your means of explaining all in this article is genuinely fastidious, every one be capable of simply know

it, Thanks a lot.

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

Saturday, May 25, 2013 2:42 PM by Mortensen

Hi there, everything is going fine here and ofcourse every one is

sharing information, that's in fact fine, keep up writing.

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

Tuesday, June 4, 2013 9:23 AM by Elizondo

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