如何在zip文件中使用相对路径?(很急,解决马上给分,在线等候)

formywish 2003-09-10 02:03:30
下面的代码实现了对指定文件夹中所有文件的压缩。压缩后的zip包中文件路径用的是绝对路径,怎样改才能实现用相对路径存储?
package zip;

import java.util.zip.*;
import java.io.*;

public class Zip {
private String sourceFolderName = null;
public void doZip(String sourceFolderName, String targetFolderName) throws Exception {
this.sourceFolderName = sourceFolderName;
String targetFileName = targetFolderName + "\\" +
sourceFolderName.substring(sourceFolderName.lastIndexOf(
"\\") + 1) + ".zip";
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(targetFileName));
go(sourceFolderName, zos);
zos.close();
}

public void go(String sourceName, ZipOutputStream zos) throws Exception {
File file = new File(sourceName);
if (file.isDirectory()) {
System.out.println("directory:" + file.getPath());
String[] entries = file.list();
String relativePath = file.getPath().substring(sourceFolderName.length());
for (int i = 0; i < entries.length; i++) {
go(file.getPath() + "\\" + entries[i], zos);
}
return;
}

byte[] buffer = new byte[4096];
int bytes_read;
FileInputStream fis = new FileInputStream(file);
ZipEntry entry = new ZipEntry(sourceName);
zos.putNextEntry(entry);
while ((bytes_read = fis.read(buffer)) != -1) {
zos.write(buffer, 0, bytes_read);
}

System.out.println("file:" + file.getPath());
fis.close();
}
public static void main(String[] argv) throws Exception{
new Zip().doZip("c:\\downloads\\ggg","c:\\downloads");
}
}


这段代码执行以后ggg.zip中的文件路径是c:\downloads\ggg\...
而我想打开ggg.zip后直接就看到的是ggg目录下的内容。
...全文
591 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
formywish 2003-09-10
  • 打赏
  • 举报
回复
兄弟,太感谢你了,呵呵。
cbhyk 2003-09-10
  • 打赏
  • 举报
回复
压缩目录的代码:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileZip
{
static final char SEPARATOR = File.separatorChar;
private int preffixLen = 0;

public void compress(OutputStream out, File file) throws IOException
{
preffixLen = file.getParentFile().getAbsolutePath().length() + 1;
ZipOutputStream zos = new ZipOutputStream(out);
addFile(zos, file);
zos.close();
}

private void addFile(ZipOutputStream zos, File file) throws IOException
{
String entryName = entryName(file);
if (entryName.equals("") || entryName.equals("."))
return;

System.out.println("adding " + entryName);
ZipEntry entry = new ZipEntry(entryName);
entry.setTime(file.lastModified());
boolean isDirectory = file.isDirectory();
if (isDirectory)
{
entry.setMethod(0);
entry.setSize(0L);
entry.setCrc(0L);
}
else
entry.setSize(file.length());

zos.putNextEntry(entry);
if (isDirectory)
{
File[] files = file.listFiles();
for(int i=0; i<files.length; i++)
addFile(zos, files[i]);
}
else
{
byte buf[] = new byte[1024];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int bytes;
while ((bytes = bis.read(buf, 0, buf.length)) != -1)
zos.write(buf, 0, bytes);
bis.close();
}
zos.closeEntry();
}

private String entryName(File file)
{
String entryName = file.getPath();
if (file.isDirectory())
entryName = entryName.endsWith(File.separator) ? entryName : entryName + File.separator;
entryName = entryName.replace(File.separatorChar, '/').substring(preffixLen);
return entryName;
}

public static void main(String args[])
{
try
{
FileOutputStream fos = new FileOutputStream("test.zip"); //压缩到test.zip文件
FileZip zip = new FileZip();
zip.compress(new BufferedOutputStream(fos), new File("c:\\downloads\\ggg")); //压缩目录c:\\downloads\\ggg
fos.close();
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧