1

To reduce file transfer time we are developing a basic WCF service which supports upload/download files from/to Sharepoint 2010 but by compressing the file before transfer. This service is using Sharepoint Server Object Model.

We are looking at ways in which we can compress the requested file and stream back to client. Instead of compressing the file to a temporary document library and then streaming the file we decided to look through the SP DLL to see if there is anything we can use. We found SPFileStream class, after looking at the code of SPFileStream using ILSpy we found it supports both in-memory and temporary file which alleviates the problem of maintain a temporary file. After trying it out we see the code works but we're not getting the expected performance improvement.

I would like to know if using SPFileStream can be used as it is used in below C# code and if is the right approach or there are better alternative approaches. Code:

SPSite site = new SPSite(request.ProjectSiteUrl);
SPWeb web = site.OpenWeb();
SPFile file = web.GetFile(request.FileServerRelativeUrl);
SPFileStream writeableStream = new SPFileStream(web, 0);

using (Stream inputStream = file.OpenBinaryStream()) {
    using (GZipStream compressedStream = new GZipStream(writeableStream, CompressionMode.Compress, true)) {
        byte[] buffer = new byte[1024 * 1024];
        int number;
        while ((number = inputStream.Read(buffer, 0, 1024 * 1024)) > 0) {
            compressedStream.Write(buffer, 0, number);
        }
    }
}

RemoteFileInfo remoteFile = new RemoteFileInfo(site);
writeableStream.Seek(0, SeekOrigin.Begin);
remoteFile.Stream = writeableStream;
return remoteFile;

Anybody who can help with this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.