压缩和解压罐子,大小不一样!
我正在为游戏制作一个修改应用程序。当我解压缩和压缩相同的文件时:由于某种原因,尽管它可以使用 7Zip 打开,但在实际应用程序中却无法打开。尽管我没有编辑解压缩的文件 - 它丢失了几个字节。 我从网上收集了这些方法,在过去的 5 个小时里一直在编辑和重试,但没有成功,有帮助吗?谢谢!
这是我的代码:
class zipper
{
static byte[] buffer = new byte[4096];
public static void unzip(File zipfile, File outputfolder) throws Exception
{
JarFile zip = new JarFile(zipfile);
Enumeration entries = zip.entries();
while(entries.hasMoreElements())
{
JarEntry entry = (JarEntry) entries.nextElement();
File unzipped = new File(outputfolder,entry.getName());
if (entry.isDirectory() && !unzipped.exists())
{
unzipped.mkdirs();
continue;
}
else if (!unzipped.getParentFile().exists())
unzipped.getParentFile().mkdirs();
InputStream in = zip.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(unzipped);
int count;
while((count = in.read(buffer, 0, buffer.length)) != -1)
fos.write(buffer, 0, count);
// clean up
fos.close();
in.close();
}
}
public static void zip(File[] infiles, JarOutputStream jos) throws Exception
{
zip(infiles,"",jos);
// clean up
jos.flush();
jos.close();
}
public static void zip(File[] infiles, String basefolder, JarOutputStream jos) throws Exception
{
FileInputStream fis = null;
for(int i=0; i<infiles.length; i++)
{
if(infiles[i].isDirectory())
{
// recursive call for directories
zip(infiles[i].listFiles(), infiles[i].getName() + File.separator, jos);
continue;
}
String filepath = basefolder + infiles[i].getName();
JarEntry entry = new JarEntry(filepath);
jos.putNextEntry(entry);
fis = new FileInputStream(infiles[i]); // get stream
int count;
while((count = fis.read(buffer, 0, buffer.length)) != -1)
jos.write(buffer, 0, count);
}
}
}
I'm making a modding application for a game. When I unzip and zip the same files: for some reason although it is open-able with 7Zip, it's not open-able in the actual application. Even though I've not edited the unzipped files - it's missing a few bytes.
I gathered the methods from the net and have been editing and retrying for the last 5 hours without success, any help guys? Thanks!
Here's my code:
class zipper
{
static byte[] buffer = new byte[4096];
public static void unzip(File zipfile, File outputfolder) throws Exception
{
JarFile zip = new JarFile(zipfile);
Enumeration entries = zip.entries();
while(entries.hasMoreElements())
{
JarEntry entry = (JarEntry) entries.nextElement();
File unzipped = new File(outputfolder,entry.getName());
if (entry.isDirectory() && !unzipped.exists())
{
unzipped.mkdirs();
continue;
}
else if (!unzipped.getParentFile().exists())
unzipped.getParentFile().mkdirs();
InputStream in = zip.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(unzipped);
int count;
while((count = in.read(buffer, 0, buffer.length)) != -1)
fos.write(buffer, 0, count);
// clean up
fos.close();
in.close();
}
}
public static void zip(File[] infiles, JarOutputStream jos) throws Exception
{
zip(infiles,"",jos);
// clean up
jos.flush();
jos.close();
}
public static void zip(File[] infiles, String basefolder, JarOutputStream jos) throws Exception
{
FileInputStream fis = null;
for(int i=0; i<infiles.length; i++)
{
if(infiles[i].isDirectory())
{
// recursive call for directories
zip(infiles[i].listFiles(), infiles[i].getName() + File.separator, jos);
continue;
}
String filepath = basefolder + infiles[i].getName();
JarEntry entry = new JarEntry(filepath);
jos.putNextEntry(entry);
fis = new FileInputStream(infiles[i]); // get stream
int count;
while((count = fis.read(buffer, 0, buffer.length)) != -1)
jos.write(buffer, 0, count);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不调试代码的情况下,我可以说最可能的原因是压缩级别。默认压缩级别是Deflater.DEFAULT_COMPRESSION。您可以使用 ZipOutputStream.setLevel() 更改它。
我相信您是使用 jar 实用程序或其他 zip 创建器创建了原始 jar 文件。然后,您使用代码创建了相同的内容并获得了不同的大小。在这种情况下,压缩级别可以解释这种差异。
Without debugging your code I can say that the most probable reason is the compression level. The default compression level is Deflater.DEFAULT_COMPRESSION. You can change it using ZipOutputStream.setLevel().
I believe that you created your original jar file using jar utility or some other zip creator. Then you created the same using your code and got different size. In this case the compression level may explain the difference.