And the documentation could be streamlined.
I had to read it various times years ago before I understood how everything works.
Not every JLink programmer is a MathLink programmer.
There should be more examples at least in the JLink folder.
I would donate two , though maybe someone can help me how to change the Do to ParallelDo, i.e.,
how to do you do parallel (master/worker) evaluation using JLink? ( I gave up ).
Also, how to do encoding right (i.e., what if you have russian characters somewher, etc.)
Here's my little package:
[font='courier new', courier, monospace]
(* Mathematica Package *)
(* Created by the Wolfram Workbench 01.11.2012 *)
BeginPackage["Unzip`",{"JLink`"}]
(* Exported symbols added here with SymbolName::usage *)
CopyRemote::usage="CopyRemote[url, localfilename] copies a file from
an http location to localfilename.";
Unzip::usage="Unzip unzips file.";
Verbose::usage="Verbose is an option to Unzip."
Begin["`Private`"]
InstallJava[];
Set[Options , {CharacterEncoding :> $CharacterEncoding, Verbose -> True}]
Unzip[zipfilein_String?FileExistsQ, dir_: Directory[], OptionsPattern[]] :=
JavaBlock[
Module[ {enum, exdir = dir, saveEntry, startdir, zf, buf, zipfile, comments, targets, target, len},
zipfile = If[DirectoryName === "", FileNameJoin[{Directory[],zipfilein}], zipfilein];
buf = JavaNew["[B", 10000];
startdir = Directory[];
createdirs[di_String] :=
JavaBlock[JavaNew["java.io.File", di][mkdirs[]]; ] /;
FileNames === {};
If[ startdir =!= dir,
If[ FileNames === {},
createdirs
];
exdir = SetDirectory
];
saveEntry[zipfi_, zipentry_, characterencoding_: $CharacterEncoding] :=
JavaBlock[
Block[ {bos, fi, fos, numRead, stream, outStream, fromcharcode, topdirle},
fi = zipentry[getName[]];
If[ zipentry[isDirectory[]],
createdirs[StringJoin[dir, $PathnameSeparator, fi]],
stream =
JavaNew["java.io.BufferedInputStream",
zipfi];
outStream =
JavaNew["java.io.BufferedOutputStream",
JavaNew["java.io.FileOutputStream",
StringJoin[dir, "/", fi]]];
While[(numRead = stream) > 0,
outStream[write[buf, 0, numRead]]];
stream[close[]];
outStream[close[]];
]
]];
zf = JavaNew["java.util.zip.ZipFile", zipfile];
len = zf[size[]];
enum = zf[entries[]];
comments = OptionValue /. Options;
targets = Table[enum[nextElement[]],{len}];
Do[
If[ comments,
Print[StringJoin["extracting: ", exdir,
$PathnameSeparator,
StringReplace[target[getName[]], "/" -> $PathnameSeparator]]]
];
saveEntry[zf, target],
{target, targets}
];
zf @ close[];
If[ startdir =!= dir,
SetDirectory
];
dir
]];
(*
Example usage:
CopyRemote["http://www.mertig.com/mathdepot/buttons/ButtonTools.nb",
ToFileName[{$UserAddOnsDirectory,"SystemFiles","FrontEnd","Palettes"},
"ButtonTools.nb"]]
*)
(* You need JLink 2.0 or higher.
this code is based on the GetRemote example in the JLink
documentation *)
Options={ProxyHost :> None, ProxyPort :> None};
CopyRemote[url_String /; StringMatchQ[url, "http://*.*", IgnoreCase-> True],
localfile_:Automatic, opts___?OptionQ] :=
(
Needs["JLink`"];
JLink`JavaBlock[
Module[ {u, stream, numRead, outFile, buf, prxyHost, prxyPort},
{prxyHost, prxyPort} = {ProxyHost, ProxyPort} /.
Flatten[{opts}] /. Options;
JLink`InstallJava[];
If[ StringQ,
(* Set properties to force use of proxy. *)
JLink`SetInternetProxy[prxyHost, prxyPort]
];
u = JLink`JavaNew["java.net.URL", url];
(* This is where the error will show up if the URL is not valid.
A Java exception will be thrown during openStream, which
causes the method to return $Failed.
*)
stream = u@openStream[];
If[ stream === $Failed,
Return[$Failed]
];
buf = JLink`JavaNew["[B", 5000];
(* 5000 is an arbitrary buffer size *)
If[ StringQ,
outFile = OpenWrite[localfile, DOSTextFormat -> False],
outFile = OpenTemporary[DOSTextFormat->False];
];
While[(numRead = stream@read) > 0,
WriteString[outFile, FromCharacterCode[If[ # < 0,
#+256,
#
]& /@
Take[JLink`Val, numRead]]]
];
stream@close[];
Close
(* Close returns the filename *)
]
] );
End[]
EndPackage[]