1

Im a long time trying to solve one problem. I have one method that Serialize a string, follows the code:

XmlRetorno()

        var algumasDef = new XmlWriterSettings {
            Indent = true, 
            OmitXmlDeclaration = true
        };
        var nameSpace = new XmlSerializerNamespaces();
        nameSpace.Add(string.Empty, "urn:sngpc-schema");
        var meuXml = new XmlSerializer(GetType(), "urn:sngpc-schema"); 

        using (var minhaString = new StringWriterWithEncoding(Encoding.GetEncoding("iso-8859-1"))) {
            using (var escreve = XmlWriter.Create(minhaString, algumasDef)) {
                meuXml.Serialize(escreve, this, nameSpace);
            }
            return minhaString.ToString();
        }

Then, my next step is to compact that string to a zip file, my method to zip.

CompactXml()

        string ziparEssaString = msg.XmlRetorno();

        byte[] byteArray = new byte[ziparEssaString.Length];
        int indexBA = 0;
        foreach (char item in ziparEssaString.ToArray()) {
            byteArray[indexBA++] = (byte)item;
        }
        //prepare to compress
        using (MemoryStream ms = new MemoryStream()) {
            using (GZipStream sw = new GZipStream(ms, CompressionMode.Compress)) {
                sw.Write(byteArray, 0, byteArray.Length);
            }

            //transform bytes[] zip to string
            byteArray = ms.ToArray();
            StringBuilder sB = new StringBuilder(byteArray.Length);
            foreach (byte item in byteArray) {
                sB.Append((char)item);
            }
            return sB.ToString();
        }

I need to compress a string that is formatted .xml and when I unpack I need the extension to be .xml too, my webservice return an error. Please, i need one light.

Edi
  • 615
  • 5
  • 15
  • I cannot understand your English. Can you try to explain what you mean by *i need this .zip with one archive on your indoor with extesion .xml. But, when i zip that string, she keeps without extesion.* Are you looking for something like this? [How to zip multiple files using only .net api in c#](http://stackoverflow.com/questions/1243929/how-to-zip-multiple-files-using-only-net-api-in-c-sharp) – dbc Apr 05 '16 at 14:18
  • im sorry, english It is not my native language. I need to compress a string to a .zip file. And when I decompress, this string has to be a file with .xml extension. When I decompress my file has no extension. – Edi Apr 05 '16 at 14:27
  • You tagged this c#-4.0. Do you specifically require a solution that works for that version? – dbc Apr 05 '16 at 15:17
  • No, any version... – Edi Apr 05 '16 at 15:42
  • Take a look at [`ZipArchive`](https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx), e.g. [ZipArchive creates invalid ZIP file](http://stackoverflow.com/questions/12347775) or [Creating a ZIP Archive in Memory Using System.IO.Compression](http://stackoverflow.com/questions/17232414/creating-a-zip-archive-in-memory-using-system-io-compression). – dbc Apr 05 '16 at 15:48

0 Answers0