Quantcast
Channel: SharpDevelop Community
Viewing all articles
Browse latest Browse all 1764

Password protectected ZIP files won't open in Windows

$
0
0

Hi,

I have created a password protected zip file using SharpZipLib, but the generated Zip file won't open in Windows.I keep getting an error "The password you entered is not correct". Also, if I try to open and extract the file in 7Zip I get the error "Unsupported compression method for 'testfile.txt'." and I'm not prompted for a password.

I'm using Windows Server 2008.

The code used to generate the Zip file is as follows:

 

 

// Start the ZIP process
using (FileStream tempFileStream = newFileStream(this.ZipFilePath + this.ZipFileName, fmOverwrite, FileAccess.ReadWrite, FileShare.None))using (ZipOutputStream zipOutput = newZipOutputStream(tempFileStream))
{
// Zip compression.zipOutput.SetLevel((int)this.ZipCompressionLevel);
Crc32 crc = newCrc32();
zipOutput.UseZip64 =
UseZip64.Off;

 

 

// Process the list of files found in the directory.
fileEntries = Directory.GetFiles(this.SourcePath, this.SourceFile, soTraverse);
foreach (string localFilename in fileEntries)
{

 // Get local path and create stream to it.
using (FileStream fileStream = newFileStream(localFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
{

 // Read full stream to in-memory buffer.
byte[ buffer = newbyte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
EntryName =
ZipEntry.CleanName(localFilename);

 

 

// Create a new entry for the current file.
ZipEntry entry = newZipEntry(EntryName);
entry.DateTime =
DateTime.Now;
entry.IsCrypted =
true;

 

 

// set Size and the crc, because the information
// about the size and crc should be stored in the header
// if it is not set it is automatically written in the footer.
// (in this case size == crc == -1 in the header)
// Some ZIP programs have problems with zip files that don't store
// the size and crc in the header.
entry.Size = fileStream.Length;
fileStream.Close();

 

 

 

// Reset and update the crc.
crc.Reset();
crc.Update(buffer);

 

 

// Update entry and write to zip stream.
entry.Crc = crc.Value;
zipOutput.Password =
this.Password.Trim();
zipOutput.PutNextEntry(entry);
zipOutput.Write(buffer, 0, buffer.Length);

 

 

// Get rid of the buffer, because this
// is a huge impact on the memory usage.
buffer = null;

 

 

 

//Close the zip file entry
zipOutput.CloseEntry();

 

}}

 

 

// Finalize the zip output.
zipOutput.Finish();

 

 

// Flushes the create and close.
zipOutput.IsStreamOwner = true;


Viewing all articles
Browse latest Browse all 1764

Trending Articles