Java-将文件添加到存档中而不删除其他文件
所以我有了这段代码,它基本上做了它应该做的事情,但是我的归档文件最终被破坏了,它无法保存文件。当然,我必须在不使用文件系统、不使用Java-将文件添加到存档中而不删除其他文件,java,zip,Java,Zip,所以我有了这段代码,它基本上做了它应该做的事情,但是我的归档文件最终被破坏了,它无法保存文件。当然,我必须在不使用文件系统、不使用TempFiles或任何东西的情况下实现这些功能。 public static void main(String[] args) throws IOException { // Path fileName = Paths.get(args[0]); // String pathZip = args[1];
TempFiles
或任何东西的情况下实现这些功能。
public static void main(String[] args) throws IOException {
// Path fileName = Paths.get(args[0]);
// String pathZip = args[1];
Path fileName = Paths.get("C:\\Users\\dell\\Desktop\\addfile.txt");
String pathZip = "C:\\Users\\dell\\Desktop\\test.zip";
Map<String, byte[]> zipEntryMap = addFilesInMap(pathZip);
zipEntryMap.forEach((zipEntryName, bytes) -> {
System.out.println(zipEntryName+" "+bytes.toString());
try {
containAndSaveSameFiles(pathZip, bytes, zipEntryName);
} catch (Exception e) {
e.printStackTrace();
}
});
// saveFileInArchive(fileName, pathZip);
}
private static Map<String, byte[]> addFilesInMap(String pathZip) throws IOException {
Map<String, byte[]> zipEntryMap = new HashMap<>();
FileInputStream fileInputStream = new FileInputStream(pathZip);
ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
ZipEntry zipEntry;
while((zipEntry = zipInputStream.getNextEntry())!= null){
byte[] buffer = new byte[1024];
ByteArrayOutputStream builder = new ByteArrayOutputStream();
int end;
while((end = zipInputStream.read(buffer)) > 0){
builder.write(buffer, 0, end);
}
zipEntryMap.put(zipEntry.getName(), builder.toByteArray());
}
return zipEntryMap;
}
private static void containAndSaveSameFiles(String pathZip, byte[] bytes, String zipEntryName) throws Exception{
ByteArrayOutputStream readBytes = new ByteArrayOutputStream();
FileOutputStream fileOutputStream = new FileOutputStream(pathZip);
ZipOutputStream outputStream = new ZipOutputStream(readBytes);
ZipEntry zipEntry2 = new ZipEntry(zipEntryName);
zipEntry2.setSize(bytes.length);
outputStream.putNextEntry(new ZipEntry(zipEntryName));
outputStream.write(bytes);
}
private static void saveFileInArchive(Path fileToBeAdded, String pathToArchive) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(pathToArchive);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
zipOutputStream.putNextEntry(new ZipEntry("new/"+fileToBeAdded.getFileName()));
Files.copy(fileToBeAdded, zipOutputStream);
zipOutputStream.close();
fileOutputStream.close();
}
publicstaticvoidmain(字符串[]args)引发IOException{
//路径文件名=Path.get(args[0]);
//字符串pathZip=args[1];
路径文件名=Path.get(“C:\\Users\\dell\\Desktop\\addfile.txt”);
String pathZip=“C:\\Users\\dell\\Desktop\\test.zip”;
Map zipEntryMap=addFilesInMap(pathZip);
forEach((zipEntryName,字节)->{
System.out.println(zipEntryName+“”+bytes.toString());
试一试{
containAndSaveSameFiles(pathZip、bytes、zipEntryName);
}捕获(例外e){
e、 printStackTrace();
}
});
//saveFileInArchive(文件名,pathZip);
}
私有静态映射addFilesInMap(字符串pathZip)引发IOException{
Map zipEntryMap=新的HashMap();
FileInputStream FileInputStream=新的FileInputStream(pathZip);
ZipInputStream ZipInputStream=新的ZipInputStream(fileInputStream);
齐彭特里;
而((zipEntry=zipInputStream.getnextery())!=null){
字节[]缓冲区=新字节[1024];
ByteArrayOutputStream生成器=新建ByteArrayOutputStream();
内端;
而((end=zipInputStream.read(buffer))>0){
写入(缓冲区,0,结束);
}
zipEntryMap.put(zipEntry.getName(),builder.toByteArray());
}
返回zipEntryMap;
}
私有静态void containandSaveSameFile(字符串pathZip,字节[]字节,字符串zipEntryName)引发异常{
ByteArrayOutputStream readBytes=新建ByteArrayOutputStream();
FileOutputStream FileOutputStream=新的FileOutputStream(pathZip);
ZipOutputStream outputStream=新ZipOutputStream(readBytes);
ZipEntry zipEntry2=新ZipEntry(zipEntryName);
zipEntry2.setSize(字节.长度);
outputStream.putNextEntry(新的ZipEntry(zipEntryName));
outputStream.write(字节);
}
私有静态void saveFileInArchive(路径filetoheaded,字符串Path toarchive)引发IOException{
FileOutputStream FileOutputStream=新的FileOutputStream(路径到存档);
ZipOutputStream ZipOutputStream=新的ZipOutputStream(fileOutputStream);
zipOutputStream.putNextEntry(新的ZipEntry(“new/”+filetobeaded.getFileName());
Files.copy(filetoheaded,zipOutputStream);
zipOutputStream.close();
fileOutputStream.close();
}
我尝试了几种方法,并在互联网上查找,但找不到任何好的答案。
谢谢您的帮助。您的代码几乎正确。
public static void main(String[] args) throws IOException {
// Path fileName = Paths.get(args[0]);
// String pathZip = args[1];
Path fileName = Paths.get("C:\\Users\\dell\\Desktop\\addfile.txt");
String pathZip = "C:\\Users\\dell\\Desktop\\test.zip";
Map<String, byte[]> zipEntryMap = addFilesInMap(pathZip);
zipEntryMap.forEach((zipEntryName, bytes) -> {
System.out.println(zipEntryName+" "+bytes.toString());
try {
containAndSaveSameFiles(pathZip, bytes, zipEntryName);
} catch (Exception e) {
e.printStackTrace();
}
});
// saveFileInArchive(fileName, pathZip);
}
private static Map<String, byte[]> addFilesInMap(String pathZip) throws IOException {
Map<String, byte[]> zipEntryMap = new HashMap<>();
FileInputStream fileInputStream = new FileInputStream(pathZip);
ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
ZipEntry zipEntry;
while((zipEntry = zipInputStream.getNextEntry())!= null){
byte[] buffer = new byte[1024];
ByteArrayOutputStream builder = new ByteArrayOutputStream();
int end;
while((end = zipInputStream.read(buffer)) > 0){
builder.write(buffer, 0, end);
}
zipEntryMap.put(zipEntry.getName(), builder.toByteArray());
}
return zipEntryMap;
}
private static void containAndSaveSameFiles(String pathZip, byte[] bytes, String zipEntryName) throws Exception{
ByteArrayOutputStream readBytes = new ByteArrayOutputStream();
FileOutputStream fileOutputStream = new FileOutputStream(pathZip);
ZipOutputStream outputStream = new ZipOutputStream(readBytes);
ZipEntry zipEntry2 = new ZipEntry(zipEntryName);
zipEntry2.setSize(bytes.length);
outputStream.putNextEntry(new ZipEntry(zipEntryName));
outputStream.write(bytes);
}
private static void saveFileInArchive(Path fileToBeAdded, String pathToArchive) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(pathToArchive);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
zipOutputStream.putNextEntry(new ZipEntry("new/"+fileToBeAdded.getFileName()));
Files.copy(fileToBeAdded, zipOutputStream);
zipOutputStream.close();
fileOutputStream.close();
}
containAndSaveSameFiles中的错误编号:1
使用readBytes而不是fileOutputStream。
通过重新打开OutputStream来重写OutputStream的saveFileInArchive
中的错误2。
检查后完成代码:
public static void main(String[] args) throws IOException {
// Path fileName = Paths.get(args[0]);
// String pathZip = args[1];
Path fileName = Paths.get("C:\\Users\\dell\\Desktop\\addfile.txt");
String pathZip = "C:\\Users\\dell\\Desktop\\test.zip";
Map<String, byte[]> zipEntryMap = addFilesInMap(pathZip);
FileOutputStream fileOutputStream = new FileOutputStream(pathZip);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
zipEntryMap.forEach((zipEntryName, bytes) -> {
System.out.println(zipEntryName+" "+bytes.toString());
try {
containAndSaveSameFiles(pathZip, bytes, zipEntryName, zipOutputStream);
} catch (Exception e) {
e.printStackTrace();
}
});
saveFileInArchive(fileName, pathZip,zipOutputStream);
zipOutputStream.close();
fileOutputStream.close();
}
private static Map<String, byte[]> addFilesInMap(String pathZip) throws IOException {
Map<String, byte[]> zipEntryMap = new HashMap<>();
FileInputStream fileInputStream = new FileInputStream(pathZip);
ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
ZipEntry zipEntry;
while((zipEntry = zipInputStream.getNextEntry())!= null){
byte[] buffer = new byte[1024];
ByteArrayOutputStream builder = new ByteArrayOutputStream();
int end;
while((end = zipInputStream.read(buffer)) > 0){
builder.write(buffer, 0, end);
}
zipEntryMap.put(zipEntry.getName(), builder.toByteArray());
}
return zipEntryMap;
}
private static void containAndSaveSameFiles(String pathZip, byte[] bytes, String zipEntryName, ZipOutputStream zipOutputStream) throws Exception{
// ByteArrayOutputStream readBytes = new ByteArrayOutputStream();
ZipEntry zipEntry2 = new ZipEntry(zipEntryName);
zipEntry2.setSize(bytes.length);
zipOutputStream.putNextEntry(new ZipEntry(zipEntryName));
zipOutputStream.write(bytes);
}
private static void saveFileInArchive(Path fileToBeAdded, String pathToArchive, ZipOutputStream zipOutputStream) throws IOException, IOException {
zipOutputStream.putNextEntry(new ZipEntry("new/"+fileToBeAdded.getFileName()));
Files.copy(fileToBeAdded, zipOutputStream);
}
publicstaticvoidmain(字符串[]args)引发IOException{
//路径文件名=Path.get(args[0]);
//字符串pathZip=args[1];
路径文件名=Path.get(“C:\\Users\\dell\\Desktop\\addfile.txt”);
String pathZip=“C:\\Users\\dell\\Desktop\\test.zip”;
Map zipEntryMap=addFilesInMap(pathZip);
FileOutputStream FileOutputStream=新的FileOutputStream(pathZip);
ZipOutputStream ZipOutputStream=新的ZipOutputStream(fileOutputStream);
forEach((zipEntryName,字节)->{
System.out.println(zipEntryName+“”+bytes.toString());
试一试{
containAndSaveSameFiles(路径zip、字节、zipEntryName、ZipoutStream);
}捕获(例外e){
e、 printStackTrace();
}
});
saveFileInArchive(文件名、路径zip、ZipoutStream);
zipOutputStream.close();
fileOutputStream.close();
}
私有静态映射addFilesInMap(字符串pathZip)引发IOException{
Map zipEntryMap=新的HashMap();
FileInputStream FileInputStream=新的FileInputStream(pathZip);
ZipInputStream ZipInputStream=新的ZipInputStream(fileInputStream);
齐彭特里;
而((zipEntry=zipInputStream.getnextery())!=null){
字节[]缓冲区=新字节[1024];
ByteArrayOutputStream生成器=新建ByteArrayOutputStream();
内端;
而((end=zipInputStream.read(buffer))>0){
写入(缓冲区,0,结束);
}
zipEntryMap.put(zipEntry.getName(),builder.toByteArray());
}
返回zipEntryMap;
}
私有静态void containandSaveSameFile(字符串pathZip,字节[]字节,字符串zipEntryName,ZipoutStream ZipoutStream)引发异常{
//ByteArrayOutputStream readBytes=新建ByteArrayOutputStream();
ZipEntry zipEntry2=新ZipEntry(zipEntryName);
zipEntry2.setSize(字节.长度);
zipOutputStream.putNextEntry(新的ZipEntry(zipEntryName));
zipOutputStream.write(字节);
}
私有静态void saveFileInArchive(路径filetoheaded,字符串Path toarchive,ZipOutputStream zipouttstream)引发IOException,IOException{
zipOutputStream.putNextEntry(新的ZipEntry(“new/”+filetobeaded.getFileName());
Files.copy(filetoheaded,zipOutputStream);
}
Mate,你是一个更安全的人。我已经花了两个小时来让它工作,但我无法理解。爱你,感谢你!