This is a ZipFile-based FileHandle implementation for LibGDX.
Created
May 3, 2013 21:28
-
-
Save MobiDevelop/5514357 to your computer and use it in GitHub Desktop.
A ZipFile-based FileHandle implementation for LibGDX.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mobidevelop.gdx.examples; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipFile; | |
import com.badlogic.gdx.Files.FileType; | |
import com.badlogic.gdx.files.FileHandle; | |
import com.badlogic.gdx.utils.GdxRuntimeException; | |
public class ArchiveFileHandle extends FileHandle { | |
final ZipFile archive; | |
final ZipEntry archiveEntry; | |
public ArchiveFileHandle (ZipFile archive, File file) { | |
super(file, FileType.Classpath); | |
this.archive = archive; | |
archiveEntry = this.archive.getEntry(file.getPath()); | |
} | |
public ArchiveFileHandle (ZipFile archive, String fileName) { | |
super(fileName.replace('\\', '/'), FileType.Classpath); | |
this.archive = archive; | |
this.archiveEntry = archive.getEntry(fileName.replace('\\', '/')); | |
} | |
@Override | |
public FileHandle child (String name) { | |
name = name.replace('\\', '/'); | |
if (file.getPath().length() == 0) return new ArchiveFileHandle(archive, new File(name)); | |
return new ArchiveFileHandle(archive, new File(file, name)); | |
} | |
@Override | |
public FileHandle sibling (String name) { | |
name = name.replace('\\', '/'); | |
if (file.getPath().length() == 0) throw new GdxRuntimeException("Cannot get the sibling of the root."); | |
return new ArchiveFileHandle(archive, new File(file.getParent(), name)); | |
} | |
@Override | |
public FileHandle parent () { | |
File parent = file.getParentFile(); | |
if (parent == null) { | |
if (type == FileType.Absolute) | |
parent = new File("/"); | |
else | |
parent = new File(""); | |
} | |
return new ArchiveFileHandle(archive, parent); | |
} | |
@Override | |
public InputStream read () { | |
try { | |
return archive.getInputStream(archiveEntry); | |
} catch (IOException e) { | |
throw new GdxRuntimeException("File not found: " + file + " (Archive)"); | |
} | |
} | |
@Override | |
public boolean exists() { | |
return archiveEntry != null; | |
} | |
@Override | |
public long length () { | |
return archiveEntry.getSize(); | |
} | |
@Override | |
public long lastModified () { | |
return archiveEntry.getTime(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mobidevelop.gdx.examples; | |
import java.util.zip.ZipFile; | |
import com.badlogic.gdx.assets.loaders.FileHandleResolver; | |
import com.badlogic.gdx.files.FileHandle; | |
public class ArchiveFileHandleResolver implements FileHandleResolver { | |
private final ZipFile archive; | |
public ArchiveFileHandleResolver (ZipFile archive) { | |
this.archive = archive; | |
} | |
@Override | |
public FileHandle resolve (String fileName) { | |
return new ArchiveFileHandle(archive, fileName); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mobidevelop.gdx.examples; | |
import java.io.IOException; | |
import java.util.zip.ZipException; | |
import java.util.zip.ZipFile; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.assets.AssetManager; | |
import com.badlogic.gdx.graphics.GL10; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.g2d.BitmapFont; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.maps.tiled.TiledMap; | |
import com.badlogic.gdx.maps.tiled.TiledMapRenderer; | |
import com.badlogic.gdx.maps.tiled.TmxMapLoader; | |
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; | |
import com.badlogic.gdx.tests.utils.GdxTest; | |
import com.badlogic.gdx.tests.utils.OrthoCamController; | |
public class ArchiveFileHandleResolverTest extends GdxTest { | |
private TiledMap map; | |
private TiledMapRenderer renderer; | |
private OrthographicCamera camera; | |
private OrthoCamController cameraController; | |
private AssetManager assetManager; | |
private BitmapFont font; | |
private SpriteBatch batch; | |
@Override | |
public void create() { | |
float w = Gdx.graphics.getWidth(); | |
float h = Gdx.graphics.getHeight(); | |
camera = new OrthographicCamera(); | |
camera.setToOrtho(false, (w / h) * 10, 10); | |
camera.zoom = 2; | |
camera.update(); | |
cameraController = new OrthoCamController(camera); | |
Gdx.input.setInputProcessor(cameraController); | |
font = new BitmapFont(); | |
batch = new SpriteBatch(); | |
try { | |
ZipFile archive = new ZipFile(Gdx.files.internal("test.zip").file()); | |
ArchiveFileHandleResolver resolver = new ArchiveFileHandleResolver(archive); | |
assetManager = new AssetManager(resolver); | |
assetManager.setLoader(TiledMap.class, new TmxMapLoader(resolver)); | |
assetManager.load("tmx/level1.tmx", TiledMap.class); | |
assetManager.finishLoading(); | |
map = assetManager.get("tmx/level1.tmx"); | |
renderer = new OrthogonalTiledMapRenderer(map, 1f / 16f); | |
} catch (ZipException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void render() { | |
Gdx.gl.glClearColor(100f / 255f, 100f / 255f, 250f / 255f, 1f); | |
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); | |
camera.update(); | |
renderer.setView(camera); | |
renderer.render(); | |
batch.begin(); | |
font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20); | |
batch.end(); | |
} | |
@Override | |
public boolean needsGL20 () { | |
return true; | |
} | |
} |
حسنا
Hello. It works on the desktop but does not work on Android. I am using the following code:
try { FileHandle fileHandle = Gdx.files.internal("data.zip"); Gdx.app.log("Assets", "exists = "+fileHandle.exists()+" / "+fileHandle.file().exists()); archive = new ZipFile(fileHandle.file()); } catch (IOException e) { e.printStackTrace(); }
and getting an error:
I/Assets: exists = true / false W/System.err: java.io.FileNotFoundException: data.zip: open failed: ENOENT (No such file or directory) at libcore.io.IoBridge.open(IoBridge.java:456) at java.io.RandomAccessFile.<init>(RandomAccessFile.java:117) at java.io.RandomAccessFile.<init>(RandomAccessFile.java:149) at java.util.zip.ZipFile.<init>(ZipFile.java:158) at java.util.zip.ZipFile.<init>(ZipFile.java:119) at com.android.miscellaneous.assets.Assets.getFileResolver(Assets.java:52) at com.android.miscellaneous.assets.Assets.getInstance(Assets.java:35) at com.android.EngineCore.create(EngineCore.java:121) at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:311) at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1520) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248) Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory) at libcore.io.Posix.open(Native Method) at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) at libcore.io.IoBridge.open(IoBridge.java:442) ... 10 more
How can I fix this issue?
I think data.zip doesn't exist, "test.zip" is used in the example above.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello. It works on the desktop but does not work on Android.
I am using the following code:
and getting an error:
How can I fix this issue?