Problems arising from File compression using system. Io. Packaging. Package

Source: Internet
Author: User

Files need to be compressed recently in the project. The compressed files are provided to users for download.

I learned that Microsoft provides a class of system. Io. Packaging. Package, which can be packaged. Then I will use it. Who knows, this is an accident.

First, check the code.

The structure is not complex. vs2010 + mvc3 is directly written in action. (This is an example) Note that system. Io. Packaging. Package is in windowsbase. dll.

Private const long buffer_size = 4096;
Public actionresult download (int id)
{
// In actual conditions, the data list will be obtained from the database based on the ID.
// This list contains the file name to be compressed
Try
{
Byte [] file = NULL;
// Assume that a download directory is used as the download Directory, which contains several files.
String folder = server. mappath ("~ /Download /");
Directoryinfo F = new directoryinfo (folder );
Using (memorystream MS = new memorystream ())
{
Using (package zip = package. Open (MS, filemode. Create) // open with memory stream
{
// In the actual situation, this will be a loop to obtain a batch of file paths to be compressed from the database
// Obtain the relative root path
Uri uri = packurihelper. createparturi (New uri ("file path", urikind. Relative ));
If (zip. partexists (URI) // if the file already exists, delete it
{
Zip. deletepart (URI );
}
Packagepart part = zip. createpart (Uri, "", compressionoption. Normal );
// Use the file stream to open the file to be compressed
Using (filestream = new filestream (Folder + "file path", filemode. Open, fileaccess. Read ))
{
Using (Stream DEST = part. getstream ())
{
// Copy data to stream
Copystream (filestream, DEST );
}
}
// The corresponding loop ends here
}
File = Ms. toarray ();
}
Return file (file, "application/X-zip-compressed", "myfile.zip ");
}
Catch (exception ex)
{

Return view ("error ");
}
}

Private Static void copystream (system. Io. Stream inputstream, system. Io. Stream outputstream)
{
Long buffersize = inputstream. Length <buffer_size? Inputstream. Length: buffer_size;
Byte [] buffer = new byte [buffersize];
Int bytesread = 0;
Long byteswritten = 0;
While (bytesread = inputstream. Read (buffer, 0, buffer. Length ))! = 0)
{
Outputstream. Write (buffer, 0, bytesread );
Byteswritten + = buffersize;
}
}

Although I do not understand the stream well, there are some general code ideas. It seems that there is no problem here. However, the results are not what I think.

First, test locally without errors.

Then deploy it in IIS, including iis7 and iis6. at the beginning, no error was reported, but then an error began.

The most frequently reported error is that the disabled stream cannot be accessed.

Another is what I forgot. The most intuitive one is that when the downloaded package is not large, it may be around 20-30 m. Once it is bigger, an error is reported.

This makes it hard for me to understand.

I set the everyone permission on a server. It's okay. When the permission of everyone is removed, the error occurs again.

In this case, consider errors.

My personal idea is: the compressed package reads data from the memory stream and writes data through the file stream. A temporary file should be generated at this time, but the size of the temporary file is limited.

You cannot continue writing. To close the file stream.

Of course, whether it is correct is unknown. Because we have not yet obtained the correct answer.

In the end, another compression method is used for compression.

Here, I will throw the problem and wait for the gods in the park to help you solve the problem.

PS: It seems that direct ouputstream does not work.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.