Page 1 of 1

Reading a text file from a zip

Posted: Tue Sep 09, 2008 3:29 pm
by miles
Sorry for the incompetence on my part, but I need help.

I am writing a cross-platform ooo extension that imports a special kind of a document into OOo Writer and saves it as an odt file.

The document is enclosed in a zip file (with a different extension), and can contain several documents of that sort. The good part is this same zip also contains a special text file, that lists all those files of that sort. So before offering the user a dialog to pick one of the contained special documents, I must parse that txt file from the zip (this file always has the same name).

I saw some code how to extract a file from a zip in ooo basic, but it does not work for me on my OSXintel_3.00m5 (although I am the administrator). The fact is I do not need to write that text document down, I just need to read it into memory and parse the contents; so how would I open a stream or string from a zip in ooo basic?

Thanks for help,
m.

Re: Reading a text file from a zip

Posted: Tue Sep 09, 2008 8:39 pm
by acknak
[Moved topic]

Re: Reading a text file from a zip

Posted: Wed Sep 10, 2008 2:49 pm
by hanya
Does this code works on your environment ? This example to get the input stream of the file in the zip archive and read text content from the stream.

Code: Select all

Sub zip_stream_test
  ' zip file
  sZipURL = "file:///E:/usr/a.zip"
  ' text file in the zip archive
  oInput = GetZipContentStream(sZipURL, "a/rp.py")
  
  If NOT IsNull(oInput) Then
    oTextInput = CreateUnoService("com.sun.star.io.TextInputStream")
    oTextInput.setInputStream(oInput)
    sText = oTextInput.readString(Array(""), False)
    oTextInput.closeInput()
    
    msgbox sText
  End If
End Sub


' sZipURL: URL of the zip archive
' sContentName: file name of the content to get its stream
Function GetZipContentStream( sZipURL As String, _
    sContentName As String ) As Object
  Dim oZipPkg As Object, oSFA As Object
  Dim oContentStream As Object, oInput As Object
  oZipPkg = CreateUnoService("com.sun.star.packages.Package")
  oZipPkg.initialize(array(sZipURL))
  If oZipPkg.hasByHierarchicalName(sContentName) Then
    oContentStream = oZipPkg.getByHierarchicalName(sContentName)
    oInput = oContentStream.getInputStream()
  End If
  GetZipContentStream = oInput
End Function

Re: Reading a text file from a zip

Posted: Wed Sep 10, 2008 8:22 pm
by miles
Great, that works just perfect (only that the variables need to be defined first, but that is a minor bug).

Thanks for your help!

m.

Re: [Solved] Reading a text file from a zip

Posted: Sat Sep 20, 2008 1:12 pm
by miles
Well, I used this code for reading a text file with rdf ending into a string and it works great.
But the second step is to load a html file (actually as if it was all just text) into a new document and then parse it's content. Now this same code doesn't work (both files are in the same zip, but when I load x.rdf it loads it into string, but x.html returns an empty string).

What could be wrong? Could I do it differently, piping the stream into an empty document?

Thanks, m.