Hi ! Is there any reliable way to create a zip fro...
# getting-started
a
Hi ! Is there any reliable way to create a zip from a folder ? I've seen 5 pages from StackOverflow and tried all the examples listed and I still come up with a zip that doesn't work with Minecraft because Minecraft is extremely strict with Zip files :/
j
Do you know exactly what's wrong with the resulting zips?
a
Not really, Minecraft just doesn't load the Datapack (which is what I'm trying to do), it says that it's loaded, but all the logic inside (functions with .mcfunction) isn't executed at all
j
Could it be because you're zipping only the files in the directory but without the directory itself? (Or the opposite?)
Did you manage to make it work by zipping by hand in some way?
a
No, I inspected the zip and it's containing all the files of my folder
Yes
Also with
unzip
package in WSL, I get false directories, directories named
data/test/functions/...
instead of a list of directories
I've found some solution that fixes this but it still doesn't load in Minecraft
p
Try attached, it uses the
java.util.zip.ZipEntry
and
java.util.zip.ZipOutputStream
from java. Usage:
Copy code
// Create 
val directoryFile = File("DirectoryPath")

val zipFile = File(parentDirFile, ZipFileName)
if (zipFile.exists()) { // Refresh it if exist
    zipFile.delete()
}
zipFile.createNewFile()
       
ZipUtil.zip(directoryFile.listFiles().toList(), zipFile.path)
My recommendation is, try to zip the directory with a working tool, then unzip the working zip file and compare it with the result you get using your code. If they are identical then must be something else.
a
I get this exception when trying to zip a folder "test" :
Copy code
Exception in thread "main" java.io.FileNotFoundException: test\data\minecraft\tags\blocks\op.json (The specified path could not be found)
	at java.base/java.io.FileInputStream.open0(Native Method)
	at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
	at ZipUtil.zipFile(ZipUtil.kt:76)
	at ZipUtil.zipDirectory(ZipUtil.kt:65)
	...
Even with library
zip4j
it's still an invalid zip file for Minecraft
p
well the above exception is telling you it could not find the file, perhaps is a restricted file and your Application process have no access to it
a
The file was generated dynamically by my application, I have no idea why it would do that 🤔
p
Do a quick research on why
Copy code
java.io.FileInputStream.open0(Native Method)
could fail to find a file. Type:
Copy code
java.io.FileNotFoundException: (The specified path could not be found)
is
op.json
a file or a directory?
a
A file
p
Ah ok, what happens if you pass the parent directory to the function, in the sample I posted change it to
Copy code
val directoryFile = File("../PathOfTheOp-JsonParent")
a
Still the same error
p
Humm, try to google the reasons for that exception is all I can say, good luck
a
Don't you have any other solutions?
j
Are you sure the file is actually generated by the time you start zipping? Could it be generated and deleted concurrently with the zipping? It would help to have a rough idea of the sequence of events you're expecting
Maybe you don't close properly the stream of the generated file (depending on how you generate it), and then have an access problem
a
the help being provided in this thread is just wow
j
@andylamax it's hard to tell whether this is sarcasm or not 😅
a
No, it is not sarcasm at all
👍 1
j
@Ayfri you said you managed to create a proper zip file recognized by Minecraft with some other tool. It would be helpful to compare that to the result of your code and check for differences. If you don't manage to do that, you can also attach both zip files here so other people can help. At the moment it's hard to tell whether there is a bug in the zipping code in the way it produces the zip, or whether it's linked to how the files are generated by your app (in which case valid zipping code fails like you showed)
a
The other way that I create a zip is using Windows native way to create zips I'll try to compare them tomorrow
👍 1
744 Views