Archived Forum Post

Index of archived forum posts

Question:

CopyToHex return null for ZipEntry

Jul 25 '14 at 11:25

I am using ChilkatDotNet4.dll (ver 9.4.1.0) and the following code returns null value

    public static string CompressToHexString(string oData)
    {
        return Zip.AppendUnicode("XX", oData).CopyToHex();
    }

The error logged for this, as far as I can tell, is: "Must be an entry in an open archive."

Searched the web for this error message and found nothing. Suggestions/Comments would be greatly appreciated.

Zip is a property that is returned as such:

private Chilkat.Zip z;
public Chilkat.Zip Zip {
get {
      if (z == null) {
          z = new Chilkat.Zip();
          z.UnlockComponent("<Unlock Code>");
      }
      return z;
    }
}

Answer

I cannot really try with the latest version, as the AppendUnicode method does not exist in the latest version, as far as I can tell. We have a license for the 9.4.1.0 version and don't know if we can use the newest version.

I am getting the ZipEntry from the following call successfully:

ZipEntry ze = Zip.AppendUnicode("XX", oData);

but when I call

ze.CopyToHex();

The LastErrorText field has the following error: "Must be an entry in an open archive."


Answer

First, you might want to try with the latest version of the library to see if that makes a difference.

If not, you should also be testing each step of the process for success/failure, and if a step fails, then you should get the contents of the LastErrorText property and post them here for review (in <pre></pre> tags).

Here's an example of the above (in VB6, but it should be easily translatable to your language):

Public Function CompressToHexString(ByVal p_Data As String) As String
   Dim lo_Entry As ChilkatZipEntry2

Set lo_Entry = Zip.AppendString("FileName", p_Data)
   If lo_Entry Is Nothing Then
      MsgBox "Error: " & Zip.LastErrorText
   Else
      CompressToHexString = lo_Entry.CopyToHex
   End If
End Function

All of that said - the error seems to indicate that you don't have an open zip file to work with. Have you created the object held in Zip, and also opened an archive using either the NewZip or OpenZip methods yet?


Answer

I'm not sure why you don't want to answer my question re: calling OpenZip/NewZip on the Zip object, or why you refuse to post the full contents of the LastErrorText property, so this will be my last attempt to help.

By the looks of the code snippet you've posted re: your ChilkatZip object instantiation (and judging by the portion of the error that you've provided), you have not called either OpenZip (on an existing ZIP file) or NewZip (to start a new zip file) before you are trying to append data to your zip object/file.


Answer

The AppendString replaces AppendUnicode. In VB.NET or C#, when a string is passed as an argument to any method, it's a string object -- and it really makes not sense to say that it's "Unicode" or anything else. It's just a string. The need to indicate the charset when a "string" is passed to a method only applies when the programming language is such that a "string" is simply a sequence of bytes, possibly null-terminated. In that case, it's important to indicate in some way how those bytes should be interpreted to arrive at the characters they represent. In C# or VB.NET, there is never such a need because strings are objects.

The CopyToHex method returns the compressed contents of the zip entry. In older versions of Chilkat Zip, it was possible to add a string to the zip object (not yet producing a zip file, but simply adding an entry to the zip object in memory), and then retrieve the compressed contents via CopyToHex. In newer versions of Chilkat Zip, this is no longer possible. It is only possible after Chilkat.Zip.OpenZip is called (meaning the .zip must pre-exist), or after calling Chilkat.Zip.WriteZip or Chilkat.Zip.WriteToMemory. (There are technical reasons why this happened, and it has to do with supporting different compression methods for ZipX.) If the need is to simply compress a string using the deflate compression algorithm, then it would be better to use the Chilkat.Compression class.


Answer

Sorry for late reply. I am colleque of drryu. We tried to use openZip or NewZip before but CopyToHex() would return null. But I tried it today and it is working fine. Wired. Thanks for help though.