It is currently Tue May 30, 2017 3:56 am



Welcome
Welcome to rfobasic

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today. **You are not required to provide truthful information to any registration questions. Be whomever you wish to be.!


Post new topic Reply to topic  [ 42 posts ]  Go to page 1, 2, 3, 4, 5  Next
Author Message
 Post subject: Zip commands
Unread postPosted: Fri Jun 24, 2016 8:15 am 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5518
Location: Paris, France
I needed native flexible commands to deal with zip/gzip/jar/apk files in rfo-compiler.

So here they are:

Zip.dir <path_sexp>, Array$[] {,<dirmark_sexp>}
Very similar to File.dir: returns the names of the files and directories inside the zip located at <path_sexp>. The path is relative to "<pref base drive>/rfo-basic/data/".
The names are placed into Array$[]. The array is sorted alphabetically with the directories at the top of the list. If the array exists, it is overwritten, otherwise a new array is created. The result is always a one-dimensional array.
A directory is identified by a marker appended to its name. The default marker is the string "(d)". You can change the marker with the optional directory mark parameter <dirmark_sexp>. If you do not want directories to be marked, set <dirmark_sexp> to an empty string, "".

Zip.open {r|w|a}, <file_table_nvar>, <path_sexp>
The zip file specified by the path string expression <path_sexp> is opened in r(ead) / w(rite) / a(ppend) mode.
The path is relative to "<pref base drive>/rfo-basic/data/".
A file table number is placed into the numeric variable <file_table_nvar>. This value is for use in subsequent Zip.read, Zip.write, or Zip.close commands.
If there was an error opening the zip file, <file_table_nvar> is set to -1 with details available in the GETERROR$() function.

Zip.close <file_table_nexp>
Closes the previously opened file.

Zip.read <file_table_nexp> ,<buffer_svar>, <file_name_sexp>
Reads the content of the file <file_name_sexp> from inside a zip and puts the result byte(s) inside the buffer string variable <buffer_svar>.
The <file_table_nexp> parameter is a file table number returned by a previous Zip.open command. If the file number is -1 then the command throws a run-time error.
If the file <file_name_sexp> is not found in the zip, <buffer_svar> is set to "EOF".
If the user tries to read the content of a zipped folder, instead of a zipped file, then the command throws a run-time error.

Zip.write <file_table_nexp>, <buffer_sexp>, <file_name_sexp>
Writes the entire contents of the string expression <buffer_sexp> into a zip, as a file named <file_name_sexp>.
The string <buffer_sexp> is assumed to be a buffer string holding binary data, typically a string coming from Byte.read.buffer on a local file.

Nicolas

_________________
- Creator of the Android BASIC! Compiler


Report this post
Top
 Profile  
 
 Post subject: Re: Zip commands
Unread postPosted: Mon Jun 27, 2016 5:05 am 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5518
Location: Paris, France
The new commands are ready, who wants to play with them?

Test version based on BASIC! v01.90.01 is available here: http://mougino.free.fr/tmp/BASIC_19001_zip.apk
(you will need to uninstall your current BASIC! and install this one).

Test program:
Code:
FN.DEF DumpToFile(buf$, path$)
  BYTE.OPEN w, bid, path$
  BYTE.WRITE.BUFFER bid, buf$
  BYTE.CLOSE bid
FN.END

FN.DEF GetFile$(path$)
  FILE.EXISTS ok, path$
  IF !ok THEN FN.RTN ""
  FILE.SIZE lof, path$
  BYTE.OPEN r, bid, path$
  BYTE.READ.BUFFER bid, lof, buf$
  BYTE.CLOSE bid
  FN.RTN buf$
FN.END

?"Zip tests"
?""

?"ZIP.DIR \"bootstrap.zip\", zip$[]"
ZIP.DIR "bootstrap.zip", zip$[]
?"Listing zip$[]:"
ARRAY.LENGTH nfiles, zip$[]
FOR i=1 TO nfiles
  ?zip$[i]
NEXT
?""

?"ZIP.OPEN r, fid, \"bootstrap.zip\""
ZIP.OPEN r, fid, "bootstrap.zip"
?"fid="+INT$(fid)
?""

?"ZIP.READ fid, buf$, \"bootstrap/fly.gif\""
ZIP.READ fid, buf$, "bootstrap/fly.gif"
?"buf$ is "+INT$(LEN(buf$))+" Bytes"
?"Dumping to \"zfly.gif\""
DumpToFile(buf$, "zfly.gif")
?""

?"ZIP.READ fid, buf$, \"bootstrap/images/icons-png/bars-black.png\""
ZIP.READ fid, buf$, "bootstrap/images/icons-png/bars-black.png"
?"buf$ is "+INT$(LEN(buf$))+" Bytes"
?"Dumping to \"zbars.png\""
DumpToFile(buf$, "zbars.png")
?""

?"ZIP.READ fid, buf$, \"bootstrap/images/""
?"Expected: runtime error (cannot 'read' a zipped folder)"
ZIP.READ fid, buf$, "bootstrap/images/"
END

ONERROR:
?GETERROR$()
?""

?"ZIP.READ fid, buf$, \"inexisting/file.ext""
?"Expected: EOF (file not found in the zip)"
ZIP.READ fid, buf$, "inexisting/file.ext"
?"buf$="+chr$(34)+buf$+chr$(34)

?"ZIP.CLOSE fid"
ZIP.CLOSE fid
?"fid="+INT$(fid)
?""

?"ZIP.OPEN w, fid, \"test.zip\""
ZIP.OPEN w, fid, "test.zip"
?"fid="+INT$(fid)
?""

?"ZIP.WRITE fid, GetFile$(\"zfly.gif\"), \"test/zfly.gif\""
buf$ = GetFile$("zfly.gif")
ZIP.WRITE fid, buf$, "test/zfly.gif"
?"Wrote "+INT$(LEN(buf$))+" Bytes"
?""

?"ZIP.WRITE fid, GetFile$(\"zbars.png\"), \"test/zbars.png\""
buf$ = GetFile$("zbars.png")
ZIP.WRITE fid, buf$, "test/zbars.png"
?"Wrote "+INT$(LEN(buf$))+" Bytes"
?""

?"ZIP.CLOSE fid"
ZIP.CLOSE fid
?"fid="+INT$(fid)
?""

Nicolas

_________________
- Creator of the Android BASIC! Compiler


Report this post
Top
 Profile  
 
 Post subject: Re: Zip commands
Unread postPosted: Mon Jun 27, 2016 5:08 am 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5518
Location: Paris, France
Oh sorry, and you'll need "bootstrap.zip" in rfo-basic/data


Attachments:
bootstrap.zip [48.94 KiB]
Downloaded 38 times

_________________
- Creator of the Android BASIC! Compiler
Report this post
Top
 Profile  
 
 Post subject: Re: Zip commands
Unread postPosted: Mon Jun 27, 2016 5:47 am 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5518
Location: Paris, France
[updated the description of the commands in the front post: Marc if you read this post, you can already copy-paste these descriptions above to the manual]

_________________
- Creator of the Android BASIC! Compiler


Report this post
Top
 Profile  
 
 Post subject: Re: Zip commands
Unread postPosted: Mon Jun 27, 2016 9:14 am 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5518
Location: Paris, France
I benchmarked my new commands and while the writing is satisfactory: the 5.5 KB gif is zipped in 6 ms ; the reading is not : the same 5.5 KB gif is unzipped in 520 ms.

I will rewrite the Zip.read tomorrow.

Nicolas

_________________
- Creator of the Android BASIC! Compiler


Report this post
Top
 Profile  
 
 Post subject: Re: Zip commands
Unread postPosted: Mon Jun 27, 2016 1:01 pm 
Offline
User avatar

Joined: Sun Nov 23, 2014 8:15 am
Posts: 2185
Location: romania
what about a 'zip.delete' ?


Report this post
Top
 Profile  
 
 Post subject: Re: Zip commands
Unread postPosted: Mon Jun 27, 2016 9:46 pm 
Offline

Joined: Wed Oct 03, 2012 9:53 am
Posts: 2797
Location: Colorado, U.S.
Nice, Nicolas. Extra thanks for doing the test code and the manual text! I haven't run your tests but I've updated the manual.

The manual text says Zip.open can take a name starting with http: and open a remote file. Is that true? Byte.open can do that, but for some reason Text.open can't.

- Marc


Report this post
Top
 Profile  
 
 Post subject: Re: Zip commands
Unread postPosted: Mon Jun 27, 2016 10:35 pm 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5518
Location: Paris, France
:oops: oops no you're right Marc, zip.open can't open a file on the network, only local.
I changed the description above.

Nicolas

_________________
- Creator of the Android BASIC! Compiler


Report this post
Top
 Profile  
 
 Post subject: Re: Zip commands
Unread postPosted: Mon Jun 27, 2016 10:59 pm 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5518
Location: Paris, France
Nice idea Gika but this can't be done "cleanly", at least there's no native Android method for that...

I did some research: there's a "hack" to delete a file without unzipping here: http://stackoverflow.com/questions/5244 ... ybe-python but it's very dirty, it consists in finding the blocks taken by the wanted file, and filling them with spaces (!)
I see 3 disadvantages: it's not clean, it's not safe, and it's not fast.

If you find a better method I'm open to implement it.

Nicolas

_________________
- Creator of the Android BASIC! Compiler


Report this post
Top
 Profile  
 
 Post subject: Re: Zip commands
Unread postPosted: Tue Jun 28, 2016 12:20 am 
Offline
User avatar

Joined: Sun Nov 23, 2014 8:15 am
Posts: 2185
Location: romania
Yes, it could be done (maybe slower), if we'd have another usefull new command "zip.create" to create a new ZIP file, if none exists.
to delete: unpack all, delete old zip, repack all but the 'deleted' ones.
Does 'zip.write' create a new zip if the zip file does not already exists? if yes, add it to manual and the 'delete' problem is solved. If not, maybe it can be implemented.
gikam


Report this post
Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 42 posts ]  Go to page 1, 2, 3, 4, 5  Next


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
suspicion-preferred