I've followed the instructions in
this thread, using the code in there I've been able to add a file to a zip file without uncompressing and recompresing it, but i have a problem, let me show you my code:
private void saveFileIntoProjectArchive(Path pathOfFile) {
this.projectArchiveFile.setWritable(true, false);
Path zipFilePath = Paths.get(this.projectArchiveFile.getAbsolutePath()),
pathToSaveInsideZIP = null;
FileSystem fs;
try {
fs = FileSystems.newFileSystem(zipFilePath, null);
pathToSaveInsideZIP = fs.getPath(pathOfFile.toString().substring((int) this.transactionalProjectFolder.getAbsolutePath().length()));
System.out.println("Coping from:\n\t"+pathOfFile+"\nto\n\t"+pathToSaveInsideZIP);
Files.copy(pathOfFile, pathToSaveInsideZIP, REPLACE_EXISTING);
System.out.println("Done!!!");
fs.close();
} catch (java.nio.file.NoSuchFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
projectArchiveFile.setWritable(false, false);
}
what I'm trying to do is, i have many files for a same project, that project is an archive (ZIP, which in the code is referenced by a java.io.File called projectArchiveFile, an instance variable of my class) containing all of those files, when I want to work with certain file inside my archive, i uncompress only that file into a folder which has an structure identical to the one inside my archive (ZIP, projectArchiveFile), the reference to that folder is a java.io.File called transactionalProjectFolder, also an instance variable of my class. But is giving me this error:
coping from:
C:\Dir1\Dir2\Dir3\Begin of Archive stucure\Another folder replica of the archive structure\An Excel File.xlsm
to
\Begin of Archive stucure\Another folder replica of the archive structure\An Excel File.xlsm
java.nio.file.NoSuchFileException: Begin of Archive stucure\Another folder replica of the archive structure\ at com.sun.nio.zipfs.ZipFileSystem.checkParents(ZipFileSystem.java:846)
at com.sun.nio.zipfs.ZipFileSystem.newOutputStream(ZipFileSystem.java:515)
at com.sun.nio.zipfs.ZipPath.newOutputStream(ZipPath.java:783)
at com.sun.nio.zipfs.ZipFileSystemProvider.newOutputStream(ZipFileSystemProvider.java:276)
at java.nio.file.Files.newOutputStream(Files.java:170)
at java.nio.file.Files.copy(Files.java:2826)
at java.nio.file.CopyMoveHelper.copyToForeignTarget(CopyMoveHelper.java:126)
at java.nio.file.Files.copy(Files.java:1222)
the rest of the stack trace are my classes.
I've been able to write in the root of the archive(zip), but whenever i try to write inside of a folder which is inside of the archive (zip) it fails, as you can notice in the stack trace, it says that java.nio.file.NoSuchFileException: Begin of Archive stucure\Another folder replica of the archive structure\ and it stops right before the name if the file which I'm trying to copy, I'M ENTIRELY SURE that the path inside the zip exist and it is appropriately spelled it just do not want to write (I've trying with Files.copy and Files.move) the file inside the archive, I've been stuck in this for a month, I don't know what else to do, any suggestion will be appreciated!!!
thanks in advance! :)...
Even tho you are sure the path exists, I would for troubleshooting add below row and see what directory structure it creates.
The error indicate the directories are missing inside the zip. Could be you are using some weird folder names not supported by zip etc..
System.out.println("Coping from:\n\t"+pathOfFile+"\nto\n\t"+pathToSaveInsideZIP);
Files.createDirectories(pathToSaveInsideZIP); // add this row
Files.copy(pathOfFile, pathToSaveInsideZIP, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Done!!!");
I'll recommend you to try out the TrueZip it's easy to use, it exposes any archive into a virtual file system, then you can append, delete or edit any file inside the archive easily.
Related
I am setting up a .jar file loader, and I want to modify a .yml File in the .jar File before I use it in a method like this. Bukkit().getPluginManager().loadPlugin(File file); If i understand it correctly, i have also have to save the changes.
I don't have the knowledge of how to do this, because I have never used files in a way like this before in Java 8. If there is somebody out, I would be very happy
Here the method where I load the jar Files, and then the place, where I want to modify and save it
for (final File file : folder.listFiles()) {// Get all files in the folder
if (!file.isDirectory()) {// Only use the file, if it is a regular file
try {
if (FileManager.isJarFile(file)) {
// found a jar file
System.out.println("Found jarfile with possible yml.file in it: " + file.getName());
/*
*
* Here it should modify the .jar file like above, bevore it gets loaded
*
*/
}
} catch (IOException e) {
// Didn't found that jar file, can ignore it
}
}
}
Short: So what should the method do? It should load the jar file, load a .yml file in it, modify the file, and then save the changes in the .jar file again.
With reference to the link: How do I read a resource file from a Java jar file?
I am trying using your code base and trying to read content of sample.csv which is residing in my project directory src/main/resources. I am unable to read the content, it says can not read file. Output:
[Can not read file: sample.csv]
//This is added within your while loop after this check /* If it is a directory, then skip it. */
I mean when file is detected then next is my below code snippet added to read the file content
if(entry.getName().contains("sample.csv")) {
File f1 = new File("sample.csv");
if(f1.canRead()) {
List<String> lines = Files.readAllLines(f1.toPath());
System.out.println("Lines in file: "+lines.size());
} else {
System.out.println("Can not read file: "+entry.getName());
}
}
Can anyone educate me what I am doing wrong here, how can I make it working?
My requirement is this:
(My micro-service) Service.jar imports Parser.jar library in its pom.xml
(My library) - Parser.jar has FnmaUtils-3.2-fieldMapping.csv file in src/main/resources directory
There is a FnmaUtils class that loads the FnmaUtils-3.2-fieldMapping.csv within its constructor, this class is part of Parser.jar - Here I am trying to read the content FnmaUtils-3.2-fieldMapping.csv, this step is keep failing with below error, tried all possible options shown in [How do I read a resource file from a Java jar file?
public FnmaUtils() {
String mappingFileUrl = null;
try {
Resource resource = new ClassPathResource("FnmaUtils-3.2-fieldMapping.csv");
mappingFileUrl = resource.getFile().getPath();
loadFnmaTemplate(mappingFileUrl);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error loading fnma template file ", e);
}
}
Getting error:
java.io.FileNotFoundException: class path resource [`FnmaUtils-3.2-fieldMapping.csv`] cannot be resolved to absolute file path because it does not reside in the file system: `jar:file:/home/ravibeli/.m2/repository/com/xxx/mismo/util/fnma-parser32/2018.1.0.0-SNAPSHOT/fnma-parser32-2018.1.0.0-SNAPSHOT.jar!/FnmaUtils-3.2-fieldMapping.csv`
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:218)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at com.xxx.fnma.util.FannieMaeUtils.<init>(FannieMaeUtils.java:41)
at com.xxx.fnma.processor.FNMA32Processor.<init>(FNMA32Processor.java:54)
at com.xxx.fnma.processor.FNMA32Processor.<clinit>(FNMA32Processor.java:43)
What is going wrong here?
Try
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream("SomeTextFile.txt");
Be sure the resource is in your classpath.
i want my jar file to access some files from itself. I know how to do this for BufferedImage but this doesn't work for other files. All i want is to extract some zips from my jar. i made a class folder in eclipse, put the zips inside and used
public File getResFile(String name){
return new File(getClass().getResource(name).getFile());
}
to get the File instance and extract it. it works fine in eclipse, but as soon as i export it to a jar it says
Exception in thread "main" java.io.FileNotFoundException: file:\C:\Users\DeLL\Desktop\BoxcraftClient\ClientInstaller.jar!\client.bxc (The filename, directory name, or volume label syntax is incorrect)
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:220)
at java.util.zip.ZipFile.<init>(ZipFile.java:150)
at java.util.zip.ZipFile.<init>(ZipFile.java:164)
at Launcher.install(Launcher.java:43)
at Launcher.main(Launcher.java:33)
Im working to fix this already something like 6 hours and can't find a solution. Please help!
There is a reason why getResource() returns a URL, and not a File, because the resource may not be a file, and since your code is packaged in the Jar file, it's not a file but a zip entry.
The only safe way to read the content of the resource, is as an InputStream, either by calling getResourceAsStream() or by calling openStream() on the returned URL.
first check your class path using java System.out.println("classpath is: " + System.getProperty("java.class.path")); to see if the classpath has your jar file.
And then use the getclass().classloader.getResourceAsStream(name). See if the returned URL is correct. Call the method isFile() on the URL to check if the URL is actually a file. And then call the getFile() method.
Use one of these methods, from the class Class
- getResource(java.lang.String)
- getResourceAsStream(java.lang.String)
this.getClass().getResource(name);
this.getClass().getResourceAsStream(name);
Warning: By default it loads the file from the location, where the this.class, is found in the package. So if using it from a class org.organisation.project.App, then the file need to be inside the jar in the directory org/organisation/project. In case the file is located in the root, or some other directory, inside the jar, use the /, in from of the file name. Like /data/names.json.
Use Spring's PathMatchingResourcePatternResolver;
It will do the trick for both launching the package from an IDE or from the file system:
public List<String> getAllClassesInRunningJar() throws Exception {
try {
List<String> list = new ArrayList<String>();
// Get all the classes inside the package com.my.package:
// This will do the work for both launching the package from an IDE or from the file system:
String scannedPackage = "com.my.package.*";
// This is spring - org.springframework.core; use these imports:
// import org.springframework.core.io.Resource;
// import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
PathMatchingResourcePatternResolver scanner = new PathMatchingResourcePatternResolver();
Resource[] resources = scanner.getResources(scannedPackage.replace(".", "/"));
for (Resource resource : resources)
list.add(resource.getURI().toString());
return list ;
} catch (Exception e) {
throw new Exception("Failed to get the classes: " + e.getMessage(), e);
}
}
My Dataflow pipeline needs to read a resource file GeoLite2-City.mmdb. I added it to my project and ran the pipeline. I confirmed that the project package zip file exists in the staging bucket on GCS.
However, when I try to read the resource file GeoLite-City.mmdb, I get a FileNotFoundException. How can I fix this? This is my code:
String path = myClass.class.getResource("/GeoLite2-City.mmdb").getPath();
File database = new File(path);
try
{
DatabaseReader reader = new DatabaseReader.Builder(database).build(); //<-this line get a FileNotFoundException
}
catch (IOException e)
{
LOG.info(e.toString());
}
My project package zip file is "classes-WOdCPQCHjW-hRNtrfrnZMw.zip"
(it contains class files and GeoLite2-City.mmdb)
The path value is "file:/dataflow/packages/staging/classes-WOdCPQCHjW-hRNtrfrnZMw.zip!/GeoLite2-City.mmdb", however it cannot be opened.
and This is the options.
--runner=BlockingDataflowPipelineRunner
--project=peak-myproject
--stagingLocation=gs://mybucket/staging
--input=gs://mybucket_log/log.68599ca3.gz
The Goal is transform the log file on GCS, and insert the transformed data to BigQuery.
When i ran locally, it was success importing to Bigquery.
i think there is a difference local PC and GCE to get the resource path.
I think the issue might be that DatabaseReader does not support paths to resources located inside a .zip or .jar file.
If that's the case, then your program worked with DirectPipelineRunner not because it's direct, but because the resource was simply located on the local filesystem rather than within the .zip file (as your comment says, the path was C:/Users/Jennie/workspace/DataflowJavaSDK-master/eclipse/starter/target/classes/GeoLite2-City.mmdb, while in the other case it was file:/dataflow/packages/staging/classes-WOdCPQCHjW-hRNtrfrnZMw.zip!/GeoLite2-City.mmdb)
I searched the web for what DatabaseReader class you might be talking about, and seems like it is https://github.com/maxmind/GeoIP2-java/blob/master/src/main/java/com/maxmind/geoip2/DatabaseReader.java .
In that case, there's a good chance that your code will work with the following minor change:
try
{
InputStream stream = myClass.class.getResourceAsStream("/GeoLite2-City.mmdb");
DatabaseReader reader = new DatabaseReader.Builder(stream).build();
}
catch (IOException e)
{
...
}
I have a folder in which there is a .dat file and one is .zip file ,I have to move the .zip file to another directory
I have two folders one is
1) c:\source folder --> having two files abc.dat and other is abc.zip
2) c:\destination ---> in which zip shpould be get copied
please advise how to achiev this what I have done rite now is ...
File directory = new File(sourceFolder);
File[] listFiles = (mcrpFilePath).listFiles();
for (File f : listFiles) {
if (f.isFile()) { // ?? here logic to pick up the zip file
//logic to move the zip file to other directory
}
}
Use File.renameTo
Renames the file denoted by this abstract pathname.
Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.
Here is Example
Or you can use Files#move (if you are using java 7)
Move or rename a file to a target file.
here is Example using move()
As simple as using the method renameTo() in File class.
public boolean renameTo(File dest);
Renames the file denoted by this abstract pathname.
Get file full path and rename it to the required location.
And make use of boolean that return by that method,to know weather it's successfully moved or not.
For detecting your zip-file:
if(f.getName.equals("abc.zip"))
or for all zip files:
if(f.getName.endsWith(".zip"))
With a regex:
if(f.getName.matches("abc*\\.zip"))
For moving it:
f.renameTo(new File("C:\dest\abc.zip");
Or, more simply:
new File("C:\src\abc.zip").renameTo("C:\dest\abc.zip");
catching exceptions as needed.
Use java.io.File and its methods to get the list of .zip files and move them (Tutorial - Moving a File or Directory).
import static java.nio.file.StandardCopyOption.*;
...
Files.move(source, target, REPLACE_EXISTING);
SOURCE