Reading a text file from a zip

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
miles
Posts: 3
Joined: Tue Sep 09, 2008 2:52 pm

Reading a text file from a zip

Post 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.
Last edited by miles on Fri Sep 26, 2008 4:48 pm, edited 3 times in total.
OOo 3.0.X on Mac OSx Leopard + Windows XP
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Reading a text file from a zip

Post by acknak »

[Moved topic]
AOO4/LO5 • Linux • Fedora 23
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Reading a text file from a zip

Post 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
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
miles
Posts: 3
Joined: Tue Sep 09, 2008 2:52 pm

Re: Reading a text file from a zip

Post 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.
OOo 3.0.X on Mac OSx Leopard + Windows XP
miles
Posts: 3
Joined: Tue Sep 09, 2008 2:52 pm

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

Post 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.
OOo 3.0.X on Mac OSx Leopard + Windows XP
Post Reply