tags:

views:

55

answers:

4

I wonder if we can use the .net class ZipPackage to zip a folder to a file.zip file. And then, I want open file.zip and add more files/folders into it. Is it possible?

[Edit]

I'm trying to use native .net library if possible

+1  A: 

The Package class is used to handle packages, which uses the zip format for storage, but has special meta files included in the zip. A package is a zip, but all zip files aren't packages. You can only open a package using the Package class, not any zip file.

A third party library would probably be better, as you are not restricted to the form of a package.

Guffa
+3  A: 

Native library is not a pure zipper, though it can be used to archive files. It adds extra file in your zip's root. If you don't mind this extra file then use it. There are other libraries, that do archiving the proper way, and are faster, easier to use, and with ore features: DotNetZip and SharpZipLib.

Dialecticus
+1  A: 

You can use the native Zip classes provided by the J# library still supported in .NET - you do have to add a reference to vjslib to your project to enable these.

Sample code:

using java.io;
using java.util.zip;

...
private void ZipFile(string sourceFile, string targetName, ref ZipOutputStream zipout)
{
    // read the temporary file to a file source
    java.io.FileInputStream fileSourceInputStream = new java.io.FileInputStream(sourceFile);

    // create a zip entry
    ZipEntry ze = new ZipEntry(targetName);
    ze.setMethod(ZipEntry.DEFLATED);
    zipout.putNextEntry(ze);

    // stream the file to the zip
    CopyStream(fileSourceInputStream, zipout);
    zipout.closeEntry();

    // flush all data to the zip
    fileSourceInputStream.close();
    zipout.flush();
}

private static void CopyStream(java.io.InputStream from, java.io.OutputStream to)
{
    BufferedInputStream input = new BufferedInputStream(from);
    BufferedOutputStream output = new BufferedOutputStream(to);
    sbyte[] buffer = new sbyte[16384];
    int got;
    while ((got = input.read(buffer, 0, buffer.Length)) > 0)
    {
        output.write(buffer, 0, got);
    }
    output.flush();
}
BrokenGlass
A: 

Thank you everyone! Any of your suggestion are very helpful to me.

Since I prefer to use native-supported .net library, I invest myself in ZipPackage class and after a while trying, my answer to my own question is yes.

At last, I have figured out how to use it to add file/folder to a package. Here's my modified version, which was derived from the sample on MSDN, that allows us to do so.

Nam Gi VU