I'm dealing with some gzipped pack200 files and have no trouble unpacking them with the command line tool. I only run into problems when I attempt to unpack the files with the pack200 library.
For reference, this is the method I am using to unpack the files:
//Output from this can be properly unpacked with command line tool
InputStream in = new GZIPInputStream(new ByteArrayInputStream(compressed));
//This is where things go awry
Pack200.Unpacker unpacker = Pack200.newUnpacker();
JarOutputStream out = new JarOutputStream(new FileOutputStream("file.jar"));
unpacker.unpack(in, out);
Here is the output of unpacker.properties():
com.sun.java.util.jar.pack.default.timezone: false
com.sun.java.util.jar.pack.disable.native: false
com.sun.java.util.jar.pack.verbose: 0
pack.class.attribute.CompilationID: RUH
pack.class.attribute.SourceID: RUH
pack.code.attribute.CharacterRangeTable: NH[PHPOHIIH]
pack.code.attribute.CoverageTable: NH[PHHII]
pack.deflate.hint: keep
pack.effort: 5
pack.keep.file.order: true
pack.modification.time: keep
pack.segment.limit: -1
pack.unknown.attribute: pass
Some other relevant information:
The jar files output by the library are consistently smaller than those unpacked by the command line tool.
The library generated files use a newer version of the .zip format (0x14 vs 0x0A).
unpack200.exe version 1.30, 07/05/05
jdk version 1.7.0_21
So to reiterate, the jar files generated by the command line tool function properly while those generated by the library do not.
I very much appreciate any help or guidance.
It was something very simple, but I'm happy to have found the problem. Here is the solution I was able to use:
//Output from this can be properly unpacked with command line tool
InputStream in = new GZIPInputStream(new ByteArrayInputStream(compressed));
//This is where things go awry
Pack200.Unpacker unpacker = Pack200.newUnpacker();
JarOutputStream out = new JarOutputStream(new FileOutputStream("file.jar"));
unpacker.unpack(in, out);
out.close();
Don't forget your JarOutPutStream.close();, kids.
Related
I have a PHP script that must unzip some uploads. The uploads are packed folders, basically zip files with a custom extension.
I am having problems with some zip files packed in one machine, but not with the same folder packed in another machine. In both cases, the compression is done with the same Java library.
This is the expected result, which then PHP further proceses:
This is the corrupted result, which makes PHP choke:
If I look at their permissions, this is what I see (01_Orig is okay, 02_Modif is corrupted):
If I look at the two packages with unzip -l (the first one is okay, the second one is corrupt):
And this is my PHP function (which is the same in both cases):
$uploads = "uploads_dir/";
$dir = new DirectoryIterator("uploads_dir/");
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$filename = $fileinfo->getFilename();
$zip = new ZipArchive;
$res = $zip->open($uploads . $fileinfo);
if ($res === TRUE) {
$zip->extractTo($uploads . $filename . "_extracted");
$zip->close();
} else {
echo "Unable to unzip";
}
}
}
Both uploads look fine when I manually unzip or open them with 7zip in my Windows machine.
If I create two hex dumps of both zip files and compare them, this is what I get: https://gist.github.com/msoutopico/22a9ef647381c2e4d26313f135c526e2
Thanks a lot in advance for any tips.
UPDATE:
In case it's relevant, the zip files are created (saved) in a linux server, and both machines where this is done (the one that works, and the one that corrupt the package) run Windows 10.
Sorted. Version 2 of the plugin was tweaked to transform path separators from \ to / in filenames. Now, even though the version 3 of the plugin was installed in both machines, in the faulty machine there was also an older one (version 1, previous to that tweak), which is the one that was being used instead of version 3. Just removing the version 1 duplicate has fixed the problem. #pmqs was right. Thank you everyone for helping me quickly solve this!
I'm currently working on a program to zip and unzip files/directories and it behaves kind of weird when I call it over the git bash.
The program takes three arguments (zip/unzip, inputPath, outputPath).
For Example:
java -jar zip_unzip.jar --zip H:\\zip_test H:\\test5\zip_test_4.zip
Everything works fine when I call it over Eclipse or CMD. It creates the directory structure if it doesn't already exist and zips the input Folder into the newly created output folder. But when I call it over the git bash like in the example above it somehow "ignores" the backslash and instead of creating a folder called test5\, creates a zip-archive called test5zip_test_4.zip
Here's a snippet of the code that takes care of creating the directory structure, where zippedFolder is the outputPath-Parameter:
File directoryToZip = new File(inputFolder);
String targetZippedFolder = zippedFolder;
targetZippedFolder = targetZippedFolder.replace("\\", "/");
//create directory to store archive in, if it doesn't already exist
File destDir = new File(targetZippedFolder.substring(0, targetZippedFolder.lastIndexOf("/")));
if (!destDir.exists()) {
destDir.mkdirs();
}
In line 3 of the codesnippet I'm replacing every backslash with a forwardslash because I thought this would make it platform-independent.
Can someone explain the behavior of the git bash to me and maybe suggest a more platform-independent path handling method?
I use the following code in a views.py
def berkeleyParser(infile,outfile):
cmd="java -Xmx1024m -jar nlptools/BerkeleyParser/berkeleyParser-1.7.jar -gr nlptools/BerkeleyParser/chn_sm5.gr < "+infile+" > "+outfile
os.system(cmd)
and then call this function to use berkeley parser.
I think the file path is ok, because the jar can successfully create the output file.
Meanwhile, I use a independent .py code to run the code above (with path modified), and got correct result in output file.
So, I don't know what's wrong with it.
I have the following problem in php when excecuting a jar file. I use the following command:
exec("java -jar JavaProject4.jar";
The JavaProject4.jar creates a txt file in a path given in the java code.
When i run the project in NetBeans the txt file is created. However, when i excecute the jar in php i don't get any errors but i can't get the file.
Here the java code I use to write the file:
public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException {
Main a = new Main();
List<Double> l1 = new ArrayList<Double>();
l1 = a.compute_features();
//System.out.println(l1);
FileWriter fstream1 = new FileWriter("C:/wamp/www/test/out.txt");
BufferedWriter out1 = new BufferedWriter(fstream1);
out1.write(l1.toString());
out1.newLine();
out1.close();
}
Im using a wamp server with php 5.2.4 and the latest java version.
Thanks a lot!
EDIT:
Problem solved, I moved the main java file in NetBeans to the default package and also fixed a wrong path and now everything is working as expected.
Thanks everyone
When you run it with PHP, how are you doing so? Are you using the PHP CLI (Command Line Interface), or are you running it through an Apache Module (CGI or otherwise)? The reason I ask, is because the problem you are having could have something to do with the user who the script is executing as. If you are using the CLI, you are running as your Windows User, however, if you are running it through Apache, then it is running as whatever user Apache is running as. Therefore, you might need to give the relevant permissions to the Apache user for that directory you are writing to.
Regards,
Ralfe
I think that you need to specify the full path for :
- the java executable
- your jar
Example :
exec("/usr/bin/java -jar /my/java/project/path/JavaProject4.jar");
I recently created an application and successfully jarred this to c:/my/folder/app.jar. It works like a charm in the following case [Startup #1]:
Open cmd
cd to c:/my/folder
java -jar app.jar
But when I do this, it doesn't work [Startup #2]:
Open cmd
cd to c:/my/
java -jar folder/app.jar
Because app.jar contains a .exe-file which I try to run in my application:
final Process p = Runtime.getRuntime().exec("rybka.exe");
It won't work in example 2 because it can't find the file rybka.exe.
Any suggestions?
Something like this is a better way forward. Copy the exe out of the jar to a temp location and run it from there. Your jar will then also be executable via webstart and so on:
InputStream src = MyClass.class.getResource("rybka.exe").openStream();
File exeTempFile = File.createTempFile("rybka", ".exe");
FileOutputStream out = new FileOutputStream(exeTempFile);
byte[] temp = new byte[32768];
int rc;
while((rc = src.read(temp)) > 0)
out.write(temp, 0, rc);
src.close();
out.close();
exeTempFile.deleteOnExit();
Runtime.getRuntime().exec(exeTempFile.toString());
If the jar will always be in that directory you can use a full path /my/folder/rybka.exe. If not, you can use getClass().getProtectionDomain().getCodeSource().getLocation() to find out the location of the jar and prepend that onto rybka.exe.
Try extracting the exe to
System.getProperty("java.io.tmpdir"));
then run it from this location too should work every time.
Paul