Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Password Encrypting a Zip File

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #19482 |

    I’ve been struggling with this one. It’s in the documentation, but the example doesn’t seem to work.

    All I wan’t to do is put a password on a zip file.

    I’m running the following code, which I converted to VB.NET:
    Dim ZipFile As ZippedFile = New ZippedFile(New DiskFile(“C:\temp\test.zip”), “C:\temp\file.txt”)
    ZipFile.OpenWrite(True, CompressionMethod.Deflated, CompressionLevel.Normal, “password”)

    I get the exception:
    Cannot perform the requested operation on an item that does not exist.
    Type: Xceed.Zip.ZippedFile
    FullName: \C:\temp\file.txt

    Somehow, the ZippedFile is getting a “\” in the full name, which I think is the problem (yes, the file.txt file exists).

    Please help.

    Imported from legacy forums. Posted by Zodman (had 3241 views)

    User (Old forums)
    Member
    Post count: 23064

    First, forget about “OpenWrite”. It’s for writing custom data to the file, not actually copying a file already on disk. Before talking about encryption, let’s clarify the way the Xceed FileSystem works:

    The ZippedFile constructor’s parameters are the actual zip file holding the data and the name of the file <b>in that zip file</b>, not the file on disk you wish to copy to that ZippedFile.

    The Xceed FileSystem is an abstract object model for copying or moving files, just as you would do on the command prompt, or using Windows Explorer. Thus, if you wish to copy “c:\temp\file.txt” into the zip file “c:\temp\test.zip”, as a file named “foobar.txt” (I’m changing the name on purpose, just for this demo), you would do something like this:

    <i>’ This is the reference to the source file
    Dim source As New DiskFile( “c:\temp\file.txt” )

    ‘ This is the reference to the destination file. It may not exist yet.
    Dim dest As New ZippedFile( New DiskFile( “c:\temp\test.zip” ), “foobar.txt” )

    ‘ Then you copy the source file onto the destination file.
    source.CopyTo( dest, true )</i>

    When you want to keep the same filenames, or copy entire folder contents, the source can be a folder, like a DiskFolder, and the destination can be a folder too, like a ZippedFolder (or a ZipArchive, nothing else than the “root” ZippedFolder for a given zip file). You could copy all “*.txt” files in “c:\temp” to the “c:\temp\test.zip” zip file like this:

    <i>’ This is the source folder
    Dim source As New DiskFolder( “c:\temp” )

    ‘ This is the destination folder
    Dim dest As New ZipArchive( New DiskFile( “c:\temp\test.zip” ) )

    ‘ You can copy matching files (recursively or not) like this:
    source.CopyFilesTo( dest, false, true, “*.txt” )</i>

    And guess what? The source can be a file, and the destination a folder. Your initial task was to copy “c:\temp\file.txt” into that zip file, with the same name? You do this:

    <i>’ This is the reference to the source file
    Dim source As New DiskFile( “c:\temp\file.txt” )

    ‘ This is the destination folder
    Dim dest As New ZipArchive( New DiskFile( “c:\temp\test.zip” ) )

    ‘ And you copy a file to a folder like this:
    source.CopyTo( dest, true )</i>

    And the best thing is that all these variables all derive from AbstractFile or AbstractFolder, thus whatever is the source or the destination, the code is the same! Unzipping “foobar.txt” from the zip file to the “c:\temp” folder is done that way:

    <i>’ This is the source file
    Dim source As New ZippedFile( New DiskFile( “c:\temp\test.zip” ), “foobar.txt” )

    ‘ This is the destination folder
    Dim dest As New DiskFolder( “c:\temp” )

    ‘ And you copy a file to a folder!
    source.CopyTo( dest, true )</i>

    Now, underneath these “CopyTo” and “CopyFilesTo” methods are actual calls to ZippedFile.OpenWrite, and you were right about your first guess: there is an overload to that method that accepts an encryption password. But since you are not calling OpenWrite yourself, the way to tell the library to use the overload which takes a password is to set the ZipArchive’s default encryption password before copying the file, like this:

    <i>’ This is the reference to the source file
    Dim source As New DiskFile( “c:\temp\file.txt” )

    ‘ This is the destination folder
    Dim dest As New ZipArchive( New DiskFile( “c:\temp\test.zip” ) )

    ‘ We want all newly copied files in that zip file to be encrypted:
    dest.DefaultEncryptionPassword = “password”

    ‘ And you copy a file to a folder like this:
    source.CopyTo( dest, true )</i>

    As for my unzipping example, you will want to set the DefaultDecryptionPassword property first before calling CopyTo on the ZippedFile. This will instruct the library to call the ZippedFile.OpenRead overload which takes a decryption password.

    Hope this helps.

    Imported from legacy forums. Posted by Martin (had 338 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks Martin, that’s very clear and concise. You did a much better job of describing how to use Xceed right there than in the entire help system!

    Incidentally, is that last example different to using QuickZip only in that you can specify the default encryption level?

    eg.

    QuickZip.Zip(“c:\temp\test.zip”, “password”, True, False, False, “c:\temp\file.txt”)

    as opposed to:

    ‘ This is the reference to the source file
    Dim source As New DiskFile(“c:\temp\file.txt”)

    ‘ This is the destination folder
    Dim dest As New ZipArchive(New DiskFile(“c:\temp\test.zip”))

    ‘ We want all newly copied files in that zip file to be encrypted:
    dest.DefaultEncryptionPassword = “password”
    dest.DefaultCompressionLevel = CompressionLevel.Highest

    ‘ And you copy a file to a folder like this:
    source.CopyTo(dest, True)

    Imported from legacy forums. Posted by Zodman (had 3557 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.