Hi,
attached is a small patch that changes zip file creation
- use just the file name of files inside a zip file loaded from an URL with
getParams ("?") instead of the whole URL
- append an unique id to files inside a zip file collection
- when adding files to a zip file collection test if a file with an identical
content has already been added and if so use that file instead
Best
Alex
----- Ursprüngliche Mail -----
> Von: "Alexander Rose" <alexander.r...@weirdbyte.de>
> An: jmol-users@lists.sourceforge.net
> Gesendet: Dienstag, 17. Juli 2012 20:32:46
> Betreff: [Jmol-users] repeated saving/loading of .jmol files
> Hi,
>
> I have a problem with saving and loading PNGJ state files to and from
> a web server. I load a pdb from the web server
> (http://host/path?getParams=pdb) and save it via the ?POST?_PNGJBIN_
> request feature. For the zip file collection the pdb file name gets
> translated to "http___host_path_getParams_pdb". The I load the PNGJ
> from the server (http://host/path?getParams=pngj1). The pdb file is
> loaded from the zip inside the pngj as
> "http://host/path?getParams=pngj1|http___host_path_getParams_pdb".
> When I again save it (http://host/path?getParams=pngj2) the pdb file
> name gets now translated to
> "http___host_path_getParams_pngj1|http___host_path_getParams_pdb".
> Upon loading pngj2 Jmol chokes on the new path, presumably because of
> the "|", at least when I added the "|" to the characters that get
> replace when creating the new file name (see below), loading of pngj2
> works. Also of note is that the pdb file name gets longer every time
> the pngj is loaded/saved. The root problem is that the
> special case of getParams ("?" character) plus loading from within a
> zip file ("|" character) is not handled. I have a solution below.
>
> I haven't experienced it yet but how I understand the code, files with
> the same name are presumed identical and only the first is added to
> the zip file. I suggest to append the file index to the file name to
> make it unique and test for identical files with a hash function.
>
>
> jmol.viewer.FileManager.java::createZipSet()
>
> String newName = name;
> if (newName.indexOf("?") > 0) // I guess this is to catch urls which
> do not have something like a file name but get params
> newName = TextFormat.replaceAllCharacters(newName, "/:?\"'=&", "_");
> // newName = TextFormat.replaceAllCharacters(newName, "|/:?\"'=&",
> "_"); // quick fix by adding "|", see above, no solution
> else
> newName = stripPath(name);
>
> where stripPath is:
>
> private static String stripPath(String name) {
> int pt = Math.max(name.lastIndexOf("|"), name.lastIndexOf("/"));
> return name.substring(pt + 1);
> }
>
>
>
> // suggested replacement to ensure filename uniqueness and fix growing
> filenames
> String newName = name;
> if (newName.indexOf("?") > 0 && !(newName.indexOf("|") > 0))
> newName = TextFormat.replaceAllCharacters(newName, "/:?\"'=&", "_");
> else
> newName = stripPath(name);
>
> newName = TextFormat.replaceAllCharacters(newName, "#", "_");
> if (newName.indexOf(".") >= 0)
> newName = newName.substring(0, newName.lastIndexOf(".")) + "#" + iFile
> + newName.substring(newName.lastIndexOf("."));
> else
> newName = newName + "#" + iFile
>
>
> with that we have
> - unique file names
> - non growing file names
> - good preserving of original file names or info where it came from
>
>
> I also started testing for unique files based on the CRC value, but
> sort of forgot that it had to be decide before replacing the new file
> names in the state... tomorrow. I'll wait for comments, then create a
> patch.
>
>
> Best
> Alexander
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond.
> Discussions
> will include endpoint security, mobile security and the latest in
> malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Jmol-users mailing list
> Jmol-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jmol-users
### Eclipse Workspace Patch 1.0
#P Jmol
Index: src/org/jmol/viewer/FileManager.java
===================================================================
--- src/org/jmol/viewer/FileManager.java (revision 17365)
+++ src/org/jmol/viewer/FileManager.java (working copy)
@@ -57,12 +57,14 @@
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
+import java.util.zip.CRC32;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.util.ArrayList;
import java.util.Date;
+import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
@@ -1229,6 +1231,8 @@
Object createZipSet(String fileName, String script, String[] scripts,
boolean includeRemoteFiles) {
List<Object> v = new ArrayList<Object>();
List<String> fileNames = new ArrayList<String>();
+ long crcValue = 0;
+ HashMap<Long,String> crcMap = new HashMap<Long,String>();
boolean haveSceneScript = (scripts != null && scripts.length == 3
&& scripts[1].startsWith("###scene.spt###"));
boolean sceneScriptOnly = (haveSceneScript && scripts[2].equals("min"));
@@ -1259,20 +1263,38 @@
int itype = urlTypeIndex(name);
boolean isLocal = (itype < 0 || itype == URL_LOCAL);
if (isLocal || includeRemoteFiles) {
- v.add(name);
String newName = name;
- if (newName.indexOf("?") > 0)
+ if (newName.indexOf("?") > 0 && !(newName.indexOf("|") > 0))
newName = TextFormat.replaceAllCharacters(newName, "/:?\"'=&", "_");
else
newName = stripPath(name);
- v.add(newName);
+ newName = TextFormat.replaceAllCharacters(newName, "#", "_");
+ // append "#iFile" to the new file name to ensure it's unique
+ if (newName.indexOf(".") >= 0)
+ newName = newName.substring(0, newName.lastIndexOf(".")) + "#" +
iFile + newName.substring(newName.lastIndexOf("."));
+ else
+ newName = newName + "#" + iFile;
if (isLocal && name.indexOf("|") < 0) {
+ v.add(name);
+ v.add(newName);
v.add(null); // data will be gotten from disk
} else {
Object ret = getFileAsBytes(name, null);
if (!(ret instanceof byte[]))
return ret;
- v.add(ret);
+ CRC32 crc = new CRC32();
+ crc.update((byte[])ret);
+ crcValue = crc.getValue();
+ // only add to the data list v when the data in the file is new
+ if(crcMap.containsKey(crcValue)){
+ // let newName point to the already added data
+ newName = crcMap.get(crcValue);
+ }else{
+ v.add(name);
+ v.add(newName);
+ v.add(ret);
+ crcMap.put(crcValue,newName);
+ }
}
name = "$SCRIPT_PATH$" + newName;
}
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Jmol-users mailing list
Jmol-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jmol-users