Telerik Forums
Telerik Document Processing Forum
2 answers
477 views
I'm trying to compress and decompress file with ZipLibrary. But it doesn't works. Can anybody help me?

 Private Function Compress(data As Byte()) As Byte()
        Dim result As Byte() = Nothing
        Try
            Using memoryStream As New MemoryStream()
                Dim method As ZipCompression = ZipCompression.Deflate64
                Using zipOutputStream As New ZipOutputStream(memoryStream, method)
                    Using writer As New StreamWriter(zipOutputStream)
                        writer.Write(data)
                        writer.Flush()
                        result = memoryStream.ToArray()
                    End Using
                End Using
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Return result
    End Function

  Private Function UnCompress(data As Byte()) As Byte()
        Dim result As Byte() = Nothing
        Try
            Using memoryStream As New MemoryStream(data)
                Using zipInputStream As New ZipInputStream(memoryStream)
                    Using reader As New StreamReader(zipInputStream)
                        result = memoryStream.ToArray
                    End Using
                End Using
            End Using
        Catch ex As Exception

        End Try
        Return result
    End Function

Stalin Vladimir
Top achievements
Rank 1
 answered on 15 Apr 2014
1 answer
137 views
Hello,

I used RadAsyncUpload control to accept a upload zip file. My zip file contains text delimiter files.  I am able to unzip it to a temporary directory, but if I try again with the same zip file I get error something like "...because it is being used by another process." (at line 17)

Below is my method to unzip:
01.private static void UnpackAndSaveFile(ZipPackageEntry entry, string destinationPath)
02.    {
03.        Stream stream = entry.OpenInputStream();
04.        var fileName = destinationPath + "\\" + entry.FileNameInZip;
05. 
06.        var directoryName = Path.GetDirectoryName(fileName);
07. 
08.        if (directoryName != null)
09.        {
10.            if (!Directory.Exists(directoryName))
11.            {
12.                Directory.CreateDirectory(directoryName);
13.            }
14. 
15.            if (System.IO.File.Exists(fileName))
16.            {
17.                System.IO.File.Delete(fileName);
18.            }
19. 
20.            StreamReader reader = new StreamReader(stream);
21.            string text = reader.ReadToEnd();
22.            File.WriteAllText(fileName, text);          
23.        }
24.    }


Please help,

Thanks,
Sam
Angel Petrov
Telerik team
 answered on 14 Apr 2014
3 answers
465 views
I have an application where I have been using the following code:

       using (ZipPackage zipPackage = ZipPackage.Open(stream, FileAccess.Read))
                    {
                        foreach (ZipPackageEntry file in zipPackage.ZipPackageEntries.ToList())
                        {
                            if (file.FileNameInZip.Equals("nsData.json"))
                            {
                                StreamReader streamReader = new StreamReader(file.OpenInputStream());
                                string jsonString = streamReader.ReadToEnd();
                            }
                       }
                 }

and it works as expected.

I have now tried to convert it to using ZipArchive instead, but I am having some problems.

I use the following code:

  using (ZipArchive archive = new ZipArchive(stream,ZipArchiveMode.Read, false, null))
                    {
                        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

                        ZipArchiveEntry entry = archive.GetEntry("nsData.json");
                        if (entry != null)
                        {
                            Stream entryStream = entry.Open();
                            StreamReader reader = new StreamReader(entryStream);
                            string content = reader.ReadToEnd();


                        }
            }

And when it comes to the last line it gives the following exception:

{Telerik.Windows.Zip.InvalidDataException: Invalid block length
   ved Telerik.Windows.Zip.DeflateDecompressor.DecodeBits()
   ved Telerik.Windows.Zip.DeflateDecompressor.DecodeUncompressedBlock(Boolean& endOfBlock)
   ved Telerik.Windows.Zip.DeflateDecompressor.CheckDecodeState()
   ved Telerik.Windows.Zip.DeflateDecompressor.Decode()
   ved Telerik.Windows.Zip.DeflateDecompressor.Inflate(Byte[] bytes, Int32 offset, Int32 length)
   ved Telerik.Windows.Zip.DeflateDecompressor.ProcessTransform(Boolean finalBlock)
   ved Telerik.Windows.Zip.DeflateTransformBase.TransformBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[] outputBuffer, Int32 outputOffset)
   ved Telerik.Windows.Zip.OperationStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   ved Telerik.Windows.Zip.CompressedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   ved System.IO.StreamReader.ReadBuffer()
   ved System.IO.StreamReader.ReadToEnd()
   ved NstPortalUCSL.ViewModel.ViewModelHandlers.DataSourceHandler.DataSouceHandler.<>c__DisplayClassb.<InitDataSources>b__8(Object sender, GetAllNstInstancesCompletedEventArgs args)
   ved NstPortalUCSL.NstPortalWsSvc.NstPortalWsClient.OnGetAllNstInstancesCompleted(Object state)}


I would be wery thankfull if you could explain to me what I am doing wrong.

Andrey
Telerik team
 answered on 04 Apr 2014
1 answer
168 views
Hi, i am ziping some .pdf files to after download, when I run at VS2010 the zip was create correct, but when deploy at server the zip was create with 1kb, the WinRar open and show an empty zip file. I don´t receive any error. If I create a small file(10MB) work well, the zip have about 70MB. But at production should be create zips with 1GB or more.

I tried in 2 servers, both with 8GB ram and Win 2008 64 bits.

Dim ArqZip As String = "Order.zip"
Dim Package As ZipPackage = ZipPackage.CreateFile(PathCart & ArqZip)
  
For Each ArqPDF In ArqsPDF
    Try
                       Package.Add(CaminhoCart & ArqPDF)
        Catch ex as exception
                        TextNotifi.Text = ex.Message
                        TextNotifi.Height = 150
                        TextNotifi.Show()
                        Exit For
        End Try
Next
  
Package.Close(False)


The ArqsPDF is a ArrayList with filenames
Maria Ilieva
Telerik team
 answered on 18 Mar 2014
2 answers
101 views
For 1 specific file I get wrong data after char 813302

It doesnt seem related to the size of the file, larger files work fine.

I do have an example that reproduces this, but so far I have only been able to reproduce it with sensitive data.

After compressing/decompressing I get:
<Field Name="Datum wijziging" Type="2" Length="0" FieldName="MUT_DAT" Hint="Datum van de laatste wijziging" RowAANdh="0" FieldName="CREATOR_ID" Hint="Creator_id" />
<Field Name="Datum aanmaak" Type="2" Length="0" FieldName="CREATEDATE" Hint="Createdate" />

In the original it said:
<Field Name="Datum aanmaak" Type="2" Length="0" FieldName="CREATEDATE" Hint="Createdate" />
<Field Name="Datum wijziging" Type="2" Length="0" FieldName="MUT_DAT" Hint="Datum van de laatste wijziging" RowGroup="88" />

So the two Field are swapped, also there is some strange tag RowAANdh in it that I cant explain, it looks like the RowGroup="88"

The code I use is below

The output I get is:

Loading file: ..\..\PowerBrowser.gpf
Length original: 5691434
Length after compress/decompress: 5657083
Index first difference: 813302 



using System.Windows;
using System.Xml.Linq;
using System;
using System.IO;
using Telerik.Windows.Zip;
 
namespace TelerikTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
 
 
        }
        public static byte[] Compress(string text)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (ZipOutputStream zipOutputStream = new ZipOutputStream(memoryStream, ZipCompression.Deflated))
                {
                    using (StreamWriter streamWriter = new StreamWriter(zipOutputStream))
                    {
                        streamWriter.Write(text);
                        //streamWriter.Flush();
                    }
                    byte[] bytes = memoryStream.ToArray();
                    return bytes;
                }
            }
        }
        public static string Decompress(byte[] data)
        {
            using (MemoryStream memoryStream = new MemoryStream(data))
            {
                using (ZipInputStream zipInputStream = new ZipInputStream(memoryStream))
                {
                    using (StreamReader streamReader = new StreamReader(zipInputStream, new System.Text.UTF8Encoding()))
                    {
                        string text = streamReader.ReadToEnd();
                        return text;
                    }
                }
            }
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MainLog.AppendText(string.Format("Loading file: {0}\n", FileTextBox.Text));
            XElement xml = XElement.Load(FileTextBox.Text);
            string text = xml.ToString();
            MainLog.AppendText(string.Format("Length original: {0}\n", text.Length));
            byte[] c = Compress(text);
            string text2 = Decompress(c);
            File.WriteAllText(@"..\..\OutputFromTelerikTest.gpf", text2);
            MainLog.AppendText(string.Format("Length after compress/decompress: {0}\n", text2.Length));
            int indexFirstDifference = -1;
            for (int i = 0; i < Math.Min(text.Length, text2.Length); i++)
            {
                if (text[i] != text2[i])
                {
                    indexFirstDifference = i;
                    break;
                }
            }
            MainLog.AppendText(string.Format("Index first difference: {0}\n", indexFirstDifference));
            MainLog.AppendText("\n");
        }
    }
}











Andrey
Telerik team
 answered on 17 Mar 2014
3 answers
1.5K+ views
Hello,
 
    I tried to compress a byte array in C# using Zip Library, but it seems I a missing something because every time I call compress sending a byte array parameter, I cannot decompress it correctly please see code as follows to compress

private string Compress(byte[] data)
       {
           MemoryStream memoryStream = new MemoryStream();
           ZipCompression method = ZipCompression.Default;
           ZipOutputStream zipOutputStream = new ZipOutputStream(memoryStream, method);
           StreamWriter writer = new StreamWriter(zipOutputStream);
            
           writer.Write(data);
           writer.Flush();
           return Convert.ToBase64String(memoryStream.ToArray());
       }

and the following is how I decompress it back and the function will return byte array as follows:

private byte[] UnCompress(string str)
        {
            MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(str));
            ZipInputStream input = new ZipInputStream(memoryStream);
            StreamReader reader = new StreamReader(input);
 
            return memoryStream.ToArray();
 
        }

Thanks,

Levi
B
Top achievements
Rank 2
 answered on 13 Mar 2014
1 answer
131 views
Where can I find the documentation for SpreadProcessing?

http://www.telerik.com/help/wpf/introduction.html Does not have anything yet.

I'm a user of UI for WPF.

Thank you.
Petya
Telerik team
 answered on 05 Mar 2014
12 answers
976 views
Hi

I not figure out how to save a (large) binary file to disk from a zip-Package back to a file.

Thanks for help
Henry
Top achievements
Rank 1
 answered on 28 Jan 2014
15 answers
586 views
Hi,

I'm trying to zip a PDF document but something goes wrong.
This is the code I use
MemoryStream compressedStream;
ZipPackage package;
MemoryStream sourcestream = new MemoryStream();
 
RadDocument document = this.CreateDocument(TileViewmodel);
this.PrepareDocument(document);
PdfFormatProvider provider = new PdfFormatProvider();
provider.Export(document, sourcestream);
 
compressedStream = new MemoryStream();
package = ZipPackage.Create(compressedStream);
package.AddStream(sourcestream, "WindowPeristedData");

Right before I add a PDF file of 7MB, the length of the compressedStream variable is 22 (bytes I think). After the AddStream the size is 142. I don't think that is correct. The sourcestream I add has a lenghth of 7.085.200. You can't zip an 7MB file into a 142 byte file.

What am I doing wrong?

Kind regards, 

Jan Buskens. 
JIG
Top achievements
Rank 1
 answered on 24 Dec 2013
3 answers
174 views
Hi,

is it possible to encrypt and decrypt a ZipLirbary which is created with the RadZipLirbary? How to?

Thanks
best Regards
Rene
Kiril Vandov
Telerik team
 answered on 29 Nov 2013
Narrow your results
Selected tags
Tags
+? more
Top users last month
Jake
Top achievements
Rank 1
RBarnes
Top achievements
Rank 1
Iron
Iron
Rami
Top achievements
Rank 1
Iron
Veteran
Nathan
Top achievements
Rank 1
Kumeri
Top achievements
Rank 1
Iron
Veteran
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Jake
Top achievements
Rank 1
RBarnes
Top achievements
Rank 1
Iron
Iron
Rami
Top achievements
Rank 1
Iron
Veteran
Nathan
Top achievements
Rank 1
Kumeri
Top achievements
Rank 1
Iron
Veteran
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?